Iterate with javascript while loops challenge

why are my not passing this challenge??

   **Your code so far**

// Setup
const myArray = [];

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

console.log(myArray);
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

That’s adding numbers in ascending order.

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

Result should be [5,4,3,2,1,0]

Hi @columbuschidozie1 !

Right now your code returns this

[ 0, 1, 2, 3, 4 ]

Which is the opposite of the correct result.

You will need to rethink your logic.

I would suggest looking at the sample they gave you again and seeing if you can modify your code to reverse the logic so you get this correct result.

[ 5, 4, 3, 2, 1, 0 ]

You will need to change the following

Hello,

the output of your code is [0,1,2,3,4], but you should create a program that outputs [5,4,3,2,1,0]l

So you need to make some changes:

  • you need to start at 5, so let i = 5
  • you need to change the condition, so the while loop is executed for number between 5 to 0 inclusive
  • you need to decrease the value of i, so i--
  • you might wannt use the myArray.push(i) inside the while loop (this will add the number in order, not placing them in the array using the index)

still not passing the challenge

let i = 5;

while (i < 5)  {

  myArray.push(i);

  i--;

}

console.log(myArray)

if you are decreasing i, will this ever not be true?

Look at the example code and think about how to reverse the logic. It starts at 0 and ends at 4, you need to start at 5 and end at 0. You have the correct start and you are decrementing the value as needed, but your end condition isn’t correct (the while condition starts off false).

let i = 5;
i < 5
// false

i do not understand what you meant by the while condition starts off false

Look at the code I posted. That is your starting value and your while condition.

Edit: just to be clear, the while loop only runs when, and as long as, the test condition evaluates to true.

that is what i have at the begining of my code

Not sure what you are referring to here?

Let us replace the while with an if statement and see if that can clear it up.

let i = 5;
if (i < 5) {
  console.log('Do I ever run?');
}

i was told to use while statement

It is just an example to show you the conditional logic is wrong.

The while condition works just like an if statement. It is evaluated and if the condition is false it never enters the body. An if statement is only evaluated one time, the while statement is evaluated at the start of each iteration. You never enter the body of your while loop.

Edit: again you want to end at 0.

You can still use a while.

The problem is your condition.

Right now you are saying 5 is less than 5

i < 5

But 5 will never be less than itself.

You first want to choose a different operator since the less than operator is not going to work.

Then you need to change the number here

If it helps to write down the logic down on pen and paper, then I would suggest that.
You first want to push 5 to the array, then 4, 3,2,1,0
Then think about how you can translate that to code for your while loop.

Hope that helps!

Hi,
you need to adjust the while condition so it runs for the numbers 5 to 0 inclusive.

let i = 5
while (i >= 0)  {
  myArray.push(i);
  i--;
}

Please don’t just blurt out answers. It doesn’t really help anyone learn. Thanks!

Ok, I just wanted to point out which part of the code is wrong (my experince is that sometimes people might to see the solution to understand what is wrong or to ask a question, but I get your point too… just acted on my personal experience of training junior devs)…
So I will try to give a hint then…

The code is running when the i < 5, so you that is what is not giving you the desired output.
Remember that you need to run the code first when the i = 5, then you decrease the value by one, so on next iteration the i = 4 snd you again decrease the value by 1… and you need it run the last time when the value is 0.

1 Like

thanks figured it out.this is what i missed >=

1 Like

If you want to use almost the same idea:

// Setup
const myArray = [];
while(i < 5) {
myArray[i] = i
i++;
}

Then try using myArray.unshift(i) which adds item in first position.

1 Like

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