Basic JavaScript: Iterate with JavaScript While Loops is not working

Tell us what’s happening:
Can anybody tell me what I’ve done wrong

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 9; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36.

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

you are adding numbers to the array from 0 to 4, instead you need to add them from 5 to 0

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

you need to change something

Hi @adeniyi.oye,

Just like @ilenia have said, you need to add them to the new array, in addition since the challenge has stayed.

  1. You need an initial variable “i” assigned to decrement.
  2. in your while condition you must have “i” to be “true” .
  3. Lastly since its descending order you must decrement the value of “i”.

Hi,

Remember that under your script you can write down console.log(myArray) so you can see the output while you try to figure it out !

Hi!

2 basic solutions exist:

  • The first one ist to take in your code into consideration what @ ieahleen said (i <= 5) and use a predefined function in Javascript to reverse the obtained array.
  • the second one is to initialize i = 5 and do similar to your code but decrementing (--) until i becomes 0.

Tell us what’s happening:
I tried everything even warätched the tutorial still didnt get the answer

Your code so far


// Setup
var myArray = [];
// Only change code below this line
var i = 5;
while(i <= 5)
while(i <= 4)
while(i <= 3)
while(i <= 2)
while(i <= 1)
while(i <= 0) {
myArray.push(i)
i++;
}
console.log(myArray)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 9; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36.

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

you are not using the while loop correctly

the while loop syntax is simple:

while (/* condition */) 
   /* code to repeat each time the condition is true */
}

are you using this syntax?
I see you have many times the while (...) part, you need it only once

1 Like

Hi @adeniyi.oye
I think there are some good things we can do here:

  1. You should only do one while loop. just like @ilenia said
while (/* condition */) 
   /* code to repeat each time the condition is true */
}
  1. In that while loop condition you should have your initial variable to be greater and = to 0.
while (initial variable (i) should be greater than and equal to  0) 
   /* code to repeat each time the condition is true */
}
  1. When you finish looping with while loop, you should decrement the initial variable, instead of increment.
i--;

Hope this helps.

Tell us what’s happening:
Why isnt my code running. Imä’m confused

Your code so far


// Setup
var myArray = [];
// Only change code below this line
var i = 5;
while(i >= 0) {
myArray.push(i)
i++;
}
console.log(myArray)

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 9; LM-X420) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Mobile Safari/537.36.

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

you have an infinite loop

your i starts at 5
it’s increased at each iteration
and i >= 0 is always true