Why is the "while" part of my code executing one time?

Tell us what’s happening:

In this do… while loop, the “do” part is working well, it’s pushing the 10 value to the array, and then adding one to i, so i === 11.
I don’t understand why the “while” part is executing…

Your code so far


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

// Only change code below this line
do {
myArray.push(i);
i++;
console.log(myArray);
console.log(i);
} while (i < 5) {
myArray.push(i);
i++;
}

console.log(myArray)

Your browser information:

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

Challenge: Iterate with JavaScript Do…While Loops

Link to the challenge:

A do...while loop will run once before the condition is checked. The do and the while aren’t separate parts and all of the loop logic should be in the do block.

1 Like

just to be clear about what Ariel said, this is not part of the loop, so it execute once as any code outside of a loop would

2 Likes

Thank you all!
Got it!!