Wrong answer passses challenge

The wrong answer to this challenge passed the challenge and i am having trouble wrapping my head around that.Could someone please explain to me why the following answer was deemed incorrect in this case:

var i = 0;

while(i < 5) {

myArray.push(i);

i++;

}

and why the following code is what was deemed correct in the challenge:

Your code so far


// Setup
var myArray = []

// Only change code below this line
var i = 5;
while(i >= 0) {
myArray.push(i);
i--;
}

Your browser information:

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

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

I don’t know what you mean by “the wrong answer passed the challenge”, but you should be able to see the difference just by logging out the array after the loop. The first creates this array:

console.log(myArray) // [0, 1, 2, 3, 4]

The second creates the array that the challenge asked for:

console.log(myArray) // [ 5, 4, 3, 2, 1, 0 ]

Add the numbers 5 through 0 (inclusive) in descending order to myArray using a while loop.

(Emphasis mine)

You count up in your while loop, which can still work, but the array method you use in that case changes - you wouldn’t add the value to the end of the array, but the beginning. And your while loop end condition excludes 5, which should be included.

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