diff --git a/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json b/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json index 569667685b..d848790253 100644 --- a/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json +++ b/challenges/02-javascript-algorithms-and-data-structures/basic-javascript.json @@ -2898,7 +2898,7 @@ "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 (myVal == 10) {
return \"Equal\";
}
return \"Not Equal\";
}
", "If 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. Once it does, however, it can compare terms as follows:", + "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
", "
", "Add the equality operator to the indicated line so that the function will return \"Equal\" when val is equivalent to 12" @@ -2947,7 +2947,8 @@ "id": "56533eb9ac21ba0edf2244d1", "title": "Comparison with the Strict Equality Operator", "description": [ - "Strict equality (===) is the counterpart to the equality operator (==). Unlike the equality operator, strict equality tests both the data type and value of the compared elements.", + "Strict equality (===) is the counterpart to the equality operator (==). However, unlike the equality operator, which attempts to convert both values being compared to a common type, the strict equality operator does not perform a type conversion.", + "If the values being compared have different types, they are considered unequal, and the strict equality operator will return false.", "Examples", "
3 === 3 // true
3 === '3' // false
", "In the second example, 3 is a Number type and '3' is a String type.", @@ -2992,6 +2993,44 @@ } } }, + { + "id": "599a789b454f2bbd91a3ff4d", + "title": "Practice comparing different values", + "description": [ + "In the last two challenges, we learned about the equality operator (==) and the strict equality operator (===). Let's do a quick review and practice using these operators some more.", + "If the values being compared are not of the same type, the equality operator will perform a type conversion, and then evaluate the values. However, the strict equalty operator will compare both the data type and value as-is, without converting one type to the other.", + "Examples", + "
3 == '3' // returns true because JavaScript performs type converstion from string to number
3 === '3' // returns false because the types are different and type conversion is not performed
", + "Note
In JavaScript, you can determine the type of a variable or a value with the typeof operator, as follows:", + "
typeof 3 // returns 'number'
typeof '3' // returns 'string'
", + "
", + "The compareEquality function in the editor compares two values using the equality operator. Modify the function so that it returns \"Equal\" only when the values are strictly equal." + ], + "releasedOn": "August 21, 2017", + "challengeSeed": [ + "// Setup", + "function compareEquality(a, b) {", + " if (a == b) { // Change this line", + " return \"Equal\";", + " }", + " return \"Not Equal\";", + "}", + "", + "// Change this value to test", + "compareEquality(10, \"10\");" + ], + "solutions": [ + "function compareEquality(a,b) {\n if (a === b) {\n return \"Equal\";\n }\n return \"Not Equal\";\n}" + ], + "tests": [ + "assert(compareEquality(10, \"10\") === \"Not Equal\", 'message: compareEquality(10, \"10\") should return \"Not Equal\"');", + "assert(compareEquality(\"20\", 20) === \"Not Equal\", 'message: compareEquality(\"20\", 20) should return \"Not Equal\"');", + "assert(code.match(/===/g), 'message: You should use the === operator');" + ], + "type": "waypoint", + "challengeType": 1, + "translations": {} + }, { "id": "56533eb9ac21ba0edf2244d2", "title": "Comparison with the Inequality Operator",