* Update iterate-with-javascript-while-loops.english.md * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com>
1.6 KiB
1.6 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
cf1111c1c11feddfaeb1bdef | Iterate with JavaScript While Loops | 1 | https://scrimba.com/c/c8QbnCM | 18220 |
Description
while
loop because it runs "while" a specified condition is true and stops once that condition is no longer true.
var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}
In the code example above, the while
loop will execute 5 times and append the numbers 0 through 4 to ourArray
.
Let's try getting a while loop to work by pushing values to an array.
Instructions
myArray
using a while
loop.
Tests
tests:
- text: You should be using a <code>while</code> loop for this.
testString: assert(code.match(/while/g));
- text: <code>myArray</code> should equal <code>[5,4,3,2,1,0]</code>.
testString: assert.deepEqual(myArray, [5,4,3,2,1,0]);
Challenge Seed
// Setup
var myArray = [];
// Only change code below this line.
After Test
if(typeof myArray !== "undefined"){(function(){return myArray;})();}
Solution
var myArray = [];
var i = 5;
while(i >= 0) {
myArray.push(i);
i--;
}