Code runs despite false condition for while in do-while loop

Tell us what’s happening:
the task is implying that only code in ‘do’ part should be run, and not the ‘while’ part. although by definition the while code shouldn’t be started because its condition is false (i is clearly 11 and while condition is i<5), it does which ruins the task.

Your code so far


// Setup
var myArray = [];
var i = 10;
console.log(i);
// Only change code below this line
do{
myArray.push(i);
i++;
console.log(i);
}

while (i < 5) {
console.log(i);
myArray.push(i);
i++;
console.log("got into while");
}
console.log(i);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36.

Challenge: Iterate with JavaScript Do…While Loops

Link to the challenge:

Your do...while is not written correctly. The do and the while should not have different code blocks. It is a single loop body. What is happening here is that the body of the while is being added combined with the code in the body of the do. The whole thing is running once, as is expected of a do...while.

const myArray = [ ]; 
let i = 0; 
console.log(i); 
// For do/while the while is a 
// break statement within the parenthesis
// So there is no code block 
// i.e. while(statement){ code } 
do  { 
myArray.push(i); 
i++; 
console.log(i); 
} while (i < 5) 
//The main difference from while and do/while
// is do/while executes code 
// in do {} then checks while(statement)
// meaning do { console.log("Here") } 
// while(false) will log "Here" at least once.
console.log(myArray);
// output: [ 0, 1, 2, 3, 4 ]
// if the start i = 10 myArray would at least be output: [ 10 ]

Here is a great resource for all things Web and also JavaScript https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/do...while