I´m struggling with while loop

*Tell us what’s happening:
Here´s the challenge: Add the numbers 5 through 0 (inclusive) in descending order to myArray using a while loop.

And here´s my solution:
// Configuração

const myArray = ;

// Altere apenas o código abaixo desta linha

let i = 5;

while(i > 0){

myArray.push(i);

i–;

}

  **Your code so far**
// Configuração
const myArray = [];

// Altere apenas o código abaixo desta linha
let i = 5;

while(i > 0){
myArray.push(i);
i--;
}

What am I doing wrong?

  **Your browser information:**

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

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

The inclusive bit means that the array should have 0 in it. The final array should look like this:

[5, 4, 3, 2, 1, 0]

Can you please show me the code?

At the end of your code you can add this line to see what your myArray looks like and debug it yourself:

console.log(myArray);

Thank you a lot, you really helped me.

This is how I (we) solved:
const myArray = ;

// Altere apenas o código abaixo desta linha
let i = 5;

while(i > -1){
myArray.push(i);
i–;
}

Good job! That way 0 will get pushed in the last iteration.

Here’s another way you could have changed your while condition. Either way works!

while (i >= 0)

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