How else do i code this to include 0 in my result pls?

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Setup
const myArray = [];

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

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

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

I think you need to think about the logic in your while condition.

Learn to be a detective. On a most basic level, when things aren’t working for me, I start logging things out:

while(i > 0) {
  console.log('entering while block, pushing i, i =', i)
  myArray.push(i);
  i--;
  console.log('leaving while block, i =', i)
}
console.log(myArray)

i don’t understand please.

What is the condition that tells the while loop that it should stop looping? What does it see on each pass? What happens after that condition is met? What will be the value of i on the start of the last iteration of the loop?

I put those log statements in there so you could see what was happening.

i got the logic now, many thanks…

1 Like

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