Iterate with JavaScript While Loops - page not responding

Iterate with JavaScript While Loops - page not responding
0

#1

Tell us what’s happening:
after entering the code and I click “Run Test” the page freezes and the page does not respond. I’ve already emptied cookies and tried opening it in Chrome Incognito… nothing seems to be working.

Your code so far


// Setup
var myArray = [];

// Only change code below this line.
var i = 0;
while(i < 5) {
    myArray.push(i);
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops


#2

You have to increase your i variable by 1 each time, when you will do it, it will loop 5 times.
Now you have infinite loop (:blush:

var i = 0;
while(i < 5) {
    myArray.push(i);
    i++;
}


#3

Hi Hisley2008,

If you have a look at your code you will notice that you are setting your variable i to 0 and then asking the code to run if variable i is less than 5. You statement always evaluates to true because variable i never becomes greater than 5 to end the loop and you create an infinite loop which causes the browser to crash.

You should add i++; underneath your myArray.push(i), so that variable i gets incremented by +1 on each loop. When variable i becomes greater than 5 the loop will end.