Iterate with JavaScript While Loop omg again?

Iterate with JavaScript While Loop omg again?
0

Tell us what’s happening:
Ive checked the video and uve checked the forum. So what is it im doing wrong here?

Your code so far


// Setup
var myArray = [];

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

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 5.1.1; KFDOWI) AppleWebKit/537.36 (KHTML, like Gecko) Silk/77.3.1 like Chrome/77.0.3865.116 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

The challenge asks you to

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

The code you have, which is the example, start from 0 and increment i as long as the value is less than 5.
Which is working and valid, but not what the challenge is asking you to do.

Hope this helps :+1:

Try adding console.log(myArray); to the end of your code. See what logs to the console and compare it with what myArray should be. (myArray should equal [5,4,3,2,1,0].)

1 Like

Idr what inclusive even means lol. Can you help with that?

Ok will try the console.log and see what happens

0 Inclusive means that you want the value 0 in the final array as well.

Or practically, you need to think carefully about the logical comparison:

< less
<= less or equal to

What im doin wrong? MyArray is empty?

var myArray = [];
var i = 5;
while(i <= 0) {
    myArray.push(i);
    i--;
}
console.log(myArray);
var i = 5; 
while(i <= 0)

The stuff inside the while loop will execute if i<=0. However, the line before that you said i=5. 5 is greater than 0 so the while loop will never execute.

1 Like

remember that the condition must be true for the loop to execute. And it must eventually become false to stop it and avoid an infinite loop.

5 <= 0 is false, the loop doesn’t execute

2 Likes

I feel myself very stupid. It should be very easy test, but I am working on it almost 1 hour.

So far I passed it with reverse method but I know it has to be without any methods, clear while loops with correct statement

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

if your i start at 5 and then go lower, when should it stop? think at that to choose your condition

then you need to design a condition that is true everytime you want your loop to run, and then false when you want your loop to stop

1 Like

lol i almost quit for the day, JFC i feel so dumb right now

var myArray = [];
var i = 5;
while(i>0){
myArray.push(i);
i--;
}

This gives me an error here on FreecodeCamp. But on other editors it works fine.

what error do you get?

this one? myArray should equal [5,4,3,2,1,0]?

think… i is 1, it is pushed to the array, then there is i--, i becomes 0. 0 > 0 is false and the loop stops

Yes thats the error I am getting.

think: you want the loop to execute when i is 0 because you want to have also the 0 in the array. Right now the condition is false when i is 0, you need to make the condition true for when i is 0

1 Like

I been working on and off this code since. And now the website wants to timeout and have to reload. Which gets annoying. Is this supposed to happen when executing this? Its annoying.

What error are you getting?

If it’s a timeout, it’s probably because you created an infinite loop, one that will execute forever since the condition never becomes false.

1 Like
// Setup
var myArray = []

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

Explanation: -1 is less than 0. While i is greater than -1. So starts at 5, push the value to the array and then decrement by i by 1

Do not use = (equal) in any for, while, do while loop. Example (while i =< 5). Why? when i = 5 it becomes infinite. Yeah, you might have i++ but why trust that? What if you then have i--?. Infinite! Do the process mentally and see what I mean: "If i = 5 then" what?

…unless, of course, you have an escape plan like break after a conditional if or switch statement, which can complicate matters the more.

BACK in the good 'ole days we had to force shutdown (power off) our computers or applications when we got stuck in an infinite loop.