Infinite while loop

Tell us what’s happening:
Why does this bring an infinite loop?

  **Your code so far**

// Setup
var myArray = [];
var i = 5;

while (i <= 5) {
myArray.push(i);
i--;
}
// Only change code below this line
  **Your browser information:**

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

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

while (i <= 5) {
  myArray.push(i);
  i--;
}

Because it is checking (i <= 5), and you are decrementing i, i will always be less than or equal to 5.

4 Likes

The condition says “while i is less than or equal to 5, loop”. And you start i at five and go down.

Ah snap

3 Likes

@kevinSmith and @DanCouper thank you for the help.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.