2.8 KiB
2.8 KiB
id, title, challengeType
id | title | challengeType |
---|---|---|
56533eb9ac21ba0edf2244d0 | Comparison with the Equality Operator | 1 |
Description
true
or false
value.
The most basic operator is the equality operator ==
. The equality operator compares two values and returns true
if they're equivalent or false
if they are not. Note that equality is different from assignment (=
), which assigns the value at the right of the operator to a variable in the left.
function equalityTest(myVal) {If
if (myVal == 10) {
return "Equal";
}
return "Not Equal";
}
myVal
is equal to 10
, the equality operator returns true
, so the code in the curly braces will execute, and the function will return "Equal"
. Otherwise, the function will return "Not Equal"
.
In order for JavaScript to compare two different data types
(for example, numbers
and strings
), it must convert one type to another. This is known as "Type Coercion". Once it does, however, it can compare terms as follows:
1 == 1 // true
1 == 2 // false
1 == '1' // true
"3" == 3 // true
Instructions
equality operator
to the indicated line so that the function will return "Equal" when val
is equivalent to 12
Tests
tests:
- text: <code>testEqual(10)</code> should return "Not Equal"
testString: 'assert(testEqual(10) === "Not Equal", ''<code>testEqual(10)</code> should return "Not Equal"'');'
- text: <code>testEqual(12)</code> should return "Equal"
testString: 'assert(testEqual(12) === "Equal", ''<code>testEqual(12)</code> should return "Equal"'');'
- text: <code>testEqual("12")</code> should return "Equal"
testString: 'assert(testEqual("12") === "Equal", ''<code>testEqual("12")</code> should return "Equal"'');'
- text: You should use the <code>==</code> operator
testString: 'assert(code.match(/==/g) && !code.match(/===/g), ''You should use the <code>==</code> operator'');'
Challenge Seed
// Setup
function testEqual(val) {
if (val) { // Change this line
return "Equal";
}
return "Not Equal";
}
// Change this value to test
testEqual(10);
Solution
function testEqual(val) {
if (val == 12) {
return "Equal";
}
return "Not Equal";
}