diff --git a/challenges/basic-javascript.json b/challenges/basic-javascript.json index c10af0def1..2dbe909815 100644 --- a/challenges/basic-javascript.json +++ b/challenges/basic-javascript.json @@ -790,6 +790,14 @@ "description":[ "You can run the same code multiple times by using a loop.", "The most common type of JavaScript loop is called a \"for loop\" because it runs \"for\" a specific number of times.", + "", + "For loops are declared with three optional expressions seperated by semicolons:", + "for([initialization]; [condition]; [final-expression])", + "The initialization statement is executed one time only before the loop starts. It is typically used to define and setup your loop varaible.", + "The condition statement is evaluated at the beginning of every loop and will continue as long as it evalutes true. When condition is false at the start of the loop, the loop will stop executing. This means if condition starts as false, your loop will never execute.", + "The final-expression is executed at the end of each loop iteration, prior to the next condition check and is usually used to increment or decrement your loop counter.", + "", + "We'll initialize with i = 0 and loop while our condition i < 5 is true. We'll increment i by 1 each loop with i++ as our final-expression.", "var ourArray = [];", "for(var i = 0; i < 5; i++) {", "  ourArray.push(i);", @@ -798,7 +806,7 @@ "Let's try getting a for loop to work by pushing values to an array." ], "tests":[ - "assert(editor.getValue().match(/for/g), 'message: You should be using a for loop for this.');", + "assert(editor.getValue().match(/for\\s*\\(/g).length > 1, 'message: You should be using a for loop for this.');", "assert.deepEqual(myArray, [0,1,2,3,4], 'message: myArray should equal [0,1,2,3,4].');" ], "challengeSeed":[ @@ -821,6 +829,84 @@ "type": "waypoint", "challengeType": 1 }, + { + "id":"56104e9e514f539506016a5c", + "title": "Iterate Odd Numbers With a For Loop", + "difficulty":"9.9824", + "description":[ + "For loops don't have to iterate one at a time. By changing our final-expression, we can count by even numbers.", + "We'll start at i = 0 and loop while i < 10. We'll increment i by 2 each loop with i += 2.", + "var ourArray = [];", + "for(var i = 0; i < 10; i += 2) {", + "  ourArray.push(i);", + "}", + "ourArray will now contain [0,2,4,6,8] ", + "Let's change our initialization and final-expression so we can count by odd numbers." + ], + "tests":[ + "assert(editor.getValue().match(/for\\s*\\(/g).length > 1, 'message: You should be using a for loop for this.');", + "assert.deepEqual(myArray, [1,3,5,7,9], 'message: myArray should equal [1,3,5,7,9].');" + ], + "challengeSeed":[ + "ourArray = [];", + "for(var i = 1; i < 10; i += 2){", + " ourArray.push(i);", + "}", + "var myArray = [];", + "", + "// Only change code below this line.", + "", + "// Push the odd numbers from one through nine to myArray using a \"for loop\" like above.", + "", + "// Only change code above this line.", + "// We use this function to show you the value of your variable in your output box.", + "// You'll learn about functions soon.", + "if(typeof(myArray) !== \"undefined\"){(function(){return myArray;})();}", + "" + ], + "type": "waypoint", + "challengeType": 1 + }, + { + "id":"56105e7b514f539506016a5e", + "title": "Count Backwards With a For Loop", + "difficulty":"9.9824", + "description":[ + "A for loop can also count backwards, so long as we can define the right conditions.", + "", + "In order to count backwards by twos, we'll need to change our initialization, condition, and final-expression.", + "We'll start at i = 10 and loop while i > 0. We'll decrement i by 2 each loop with i -= 2.", + "var ourArray = [];", + "for(var i = 10; i > 0; i -= 2) {", + "  ourArray.push(i);", + "}", + "ourArray will now contain [10,8,6,4,2] ", + "Let's change our initialization and final-expression so we can count backward by twos for numbers." + ], + "tests":[ + "assert(editor.getValue().match(/for\\s*\\(/g).length > 1, 'message: You should be using a for loop for this.');", + "assert.deepEqual(myArray, [9,7,5,3,1], 'message: myArray should equal [9,7,5,3,1].');" + ], + "challengeSeed":[ + "ourArray = [];", + "for(var i = 9; i > 0; i -= 2){", + " ourArray.push(i);", + "}", + "var myArray = [];", + "", + "// Only change code below this line.", + "", + "// Push the odd numbers from nine through one to myArray using a \"for loop\" like above.", + "", + "// Only change code above this line.", + "// We use this function to show you the value of your variable in your output box.", + "// You'll learn about functions soon.", + "if(typeof(myArray) !== \"undefined\"){(function(){return myArray;})();}", + "" + ], + "type": "waypoint", + "challengeType": 1 + }, { "id":"cf1111c1c11feddfaeb1bdef", "title": "Iterate with JavaScript While Loops",