diff --git a/challenges/02-javascript-algorithms-and-data-structures/es6.json b/challenges/02-javascript-algorithms-and-data-structures/es6.json index b3370ed019..695eb91080 100644 --- a/challenges/02-javascript-algorithms-and-data-structures/es6.json +++ b/challenges/02-javascript-algorithms-and-data-structures/es6.json @@ -75,33 +75,33 @@ "
var numArray = [];", "With the
for (var i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
var
keyword, i
is declared globally. So when i++
is executed, it updates the global variable. This code is similiar to the following:",
"var numArray = [];", - "This behavior will cause problems when you create a function and store it for later use inside the for loop that uses the
var i;
for (i = 0; i < 3; i++) {
numArray.push(i);
}
console.log(numArray);
// returns [0, 1, 2]
console.log(i);
// returns 3
i
variable. This is because the stored function will always refer to the value of the updated global i
variable.",
+ "This behavior will cause problems if you were to create a function and store it for later use inside a for loop that uses the i
variable. This is because the stored function will always refer to the value of the updated global i
variable.",
"var printNumTwo;", "As you can see,
for (var i = 0; i < 3; i++) {
if(i === 2){
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 3
printNumTwo()
prints 3 and not 2. This is because the value assigned to i
was updated and the printNumTwo()
returns the global i
and not the value i
had when the function was created in the for loop. The let
keyword does not follow this behavior:",
"'use strict';", "
let printNumTwo;
for (let i = 0; i < 3; i++) {
if (i === 2) {
printNumTwo = function() {
return i;
};
}
}
console.log(printNumTwo());
// returns 2
console.log(i);
// returns \"i is not defined\"
i
is not defined because it was not declared in the global scope. It is only declared within the for loop statement. printNumTwo()
returned the correct value because three different i
variables with unique values (0, 1, and 2) were created by the let
keyword within the loop statement.",
"\"use strict\";
to the top of your code."
+ "Fix the code so that i
declared in the if statement is a separate variable than i
declared in the first line of the function. Be certain not to use the var
keyword anywhere in your code.",
+ "Note\"use strict\";
to the top of your code.",
+ "This exercise is designed to illustrate the difference between how var
and let
keywords assign scope to the declared variable. When programming a function similar to the one used in this exercise, it is often better to use different variable names to avoid confusion."
],
"challengeSeed": [
- "var newCampers = [{camper: \"Wil\"}, {camper: \"Sam\"}, {camper: \"Dav\"}];",
- "// only change code below this line",
- "for (var i = 0; i < newCampers.length; i++) {",
- "// only change code above this line",
- " newCampers[i].roleCall = function() {",
- " return \"Camper # \" + (i + 1) + \" has arrived.\";",
- " };",
+ "function checkScope() {",
+ " var i = \"function scope\";",
+ " if (true) {",
+ " i = \"block scope\";",
+ " console.log(\"Block scope i is: \", i);",
+ " }",
+ " console.log(\"Function scope i is: \", i);",
+ " return i;",
"}",
- "// test your code",
- "console.log(newCampers[0].roleCall());",
- "console.log(newCampers[1].roleCall());",
- "console.log(newCampers[2].roleCall());"
+ "// only change the code above this line",
+ "checkScope();"
],
"tests": [
- "assert(newCampers[0].roleCall() === \"Camper # 1 has arrived.\", 'message: newCampers[0].call()
should call the index of the first camper');",
- "assert(newCampers[1].roleCall() === \"Camper # 2 has arrived.\", 'message: newCampers[1].call()
should call the index of the second camper');",
- "assert(newCampers[2].roleCall() === \"Camper # 3 has arrived.\", 'message: newCampers[2].call()
should call the index of the third camper');"
+ "// TEMPORARILY COMMENTED OUT: assert(!/var/g.test(code) && /let/g.test(code), 'message: The var
keyword should be replaced with let
. (This test is temporarily disabled)');",
+ "assert(code.match(/(i\\s*=\\s*).*\\s*.*\\s*.*\\1('|\")block\\s*scope\\2/g), 'message: The variable i
declared in the if statement should equal \"block scope\".');",
+ "assert(checkScope() === \"function scope\", 'message: checkScope()
should return \"function scope\"');"
],
"type": "waypoint",
"challengeType": 1,