Help im super stucked

Tell us what’s happening:

So I was doing this challenge : https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/iterate-with-javascript-while-loops
or “Basic JavaScript: Iterate with JavaScript While Loops”.

And i dont know how to finish it because is asking me to return [5,4,3,2,1,0] .instead of [ 0, 1, 2, 3, 4 ] as the guy who answers the challenge does in watch a video.

Im stucked and dont know how to do this, can any one help me?

Your code so far

js

'var myArray = [];

var i = 0;
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/79.0.3945.88 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

it also collapses when im trying to write new code even thoug is not the real answer im just trying some opitions but every time i try something new the page collpases, i think theres a bug in this challenge

If the page is crashing while you are working on it you are probably creating an infinite loop. Always be very cautious about using a while loop because it is much easier to write an infinite loop. Consider using a for loop instead.

1 Like

@ArielLeslie You have to use a while loop for this challenge.

You should be using a while loop for this.

@saenzcast What do you mean by the page is collapsing? As said, you have to make sure you do not have an infinite loop.

Look at the example code and think about how you would reverse the logic so it starts at 5 and decrements down to 0

1 Like

You are asked to add numbers from 5 to 0, so you probably want to reverse your logic and start from var i = 5; :wink:

hey thank you guys at the end i did not found the way but saw a comments regarding this challenge that uses the reverse thingy :

var myArray = ;
var i = 0;
while(i <= 5) {
myArray.push(i);
i++;
}
myArray.reverse();
console.log(myArray);

that solved the challenge , thanks for responding, i did not think that anyone would jaja

hey! thanks man! i will try it but i fixed it with this

var myArray = ;
var i = 0;
while(i <= 5) {
myArray.push(i);
i++;
}
myArray.reverse();
console.log(myArray);

There is no reason to do this. Start the value of i at the correct value and adjust it accordingly.

To put @RandellDawson’s advice differently, try counting down instead of counting up.

1 Like

totally true thanks!

understood captain! thank you very much