Basic JavaScript: Iterate with JavaScript While Loops Help

Tell us what’s happening:
This isn’t passing on FCC, but will pass on the console in Chrome . the output is what is required by FCC to pass

Your code so far


// Setup
var myArray = [];

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

output … (6) [5, 4, 3, 2, 1, 0]

Your browser information:

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

Challenge: Iterate with JavaScript While Loops

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops

1 Like

Hello!

Your initiation of i is not correct.

Your while loop should loop from 5 to 0…

2 Likes

When you say my initiation of my variable is not correct, Im not sure what you mean? Can you describe initiation another way? The array is displayed, counting down form 5…0.

@desmoquak
Your loop starts with 6 because

so that means that the first number added to the myArray will be 6.

That will repeat while i >= 0 and will stop when i = 0.
myArray = [6,5,4,3,2,1,0] - your result

As the challenge says,myArray should be
myArray = [5,4,3,2,1,0]

Can you see the difference? 6 is redundant in this case and i should start when is 5,not 6.

Hope this helps,happy coding! :slight_smile:

2 Likes

NIna1012,

Thank you! it worked!