Tell us what happening here? plzzzz

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Setup
var myArray = [];
var i = 0;
// Only change code below this line
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/91.0.4472.106 Safari/537.36

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

Hello there.

Do you have a question?

If so, please edit your post to include it in the Tell us what’s happening section.

Learning to describe problems is an important part of learning how to code.

Also, the more information you give us, the more likely we are to be able to help.

2 Likes
  1. With the condition i < 5 your array will be missing the last number you need to add. Because it only runs when i is less than 5, not when i is 5.

  2. You are pushing the numbers in the wrong order. You either need to use unshift or start at the other end, adjust the condition and decrement instead.

var myArray = [];
var i = 0;

while (i < 5) {
  console.log(i) // 0, 1, 2, 3, 4
  myArray.push(i);
  i++;
}
console.log(myArray); // [ 0, 1, 2, 3, 4 ]

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

I also do but it can’t run but output 5,4,3,2,1

Please post your current code.

I closed your other thread, do not post duplicate topics. Post the new code you have in this thread.

As a reminder, the condition in the while must be true. This i > 0 does not include 0.

Hi @Roshan120 ,

I saw your earlier post where in your code, the while condition was (i > 0).

As per the instructions, myArray should equal [5,4,3,2,1,0].

In your code,myArray does not include 0. Its equal to [5,4,3,2,1].
Adjust your while condition accordingly and it should pass the tests.

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