Iterate with JavaScript While Loops Help 2

I have tried this challenge but both the video and the other searchable answers seem to be out of date and I have now become stuck. I have shown my code below that makes the most logical sense to me but I can’t get it to accept it. Please anyone can help with the correct answer to the problem? Thanks.

  **Your code so far**
// Setup

var myArray = [];

// Only change code below this line

var i = 6;

while(i < 6) {

myArray.push(i);

i--;

};

// Setup
var myArray = [];

// Only change code below this line

var i = 6;
while(i < 6) {
myArray.push(i);
i--;
};
  **Your browser information:**

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36

Challenge: Iterate with JavaScript While Loops

Link to the challenge:

First thing, if i==6, i is not < 6, so thewhile loop doesn’t happen. Second thing, i- isn’t the same as i--.

Hi thanks, I actually thought I did something different to what I posted above, I will come back when my brain fog has cleared.

1 Like

So this was the right answer:

// Setup

var myArray = [];

// Only change code below this line

var i = 5;

while(i >= 0) {

myArray.push(i);

i--;

};

Whereas I thought it should be:

while ( i <= 5)

This was in order to make it go from 5 through to 0 inclusive.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

1 Like

Ok will do.

But can you tell me why my initial thoughts were wrong and why it caused an infinite loop?

Your while loop in the first post never ran because the condition was never true.

var myArray = [];

var i = 6;
console.log(i < 6); // false

while (i < 6) {
  console.log('never runs');
  myArray.push(i);
  i--;
}

var myArray = [];

// Only change code below this line

var i = 5;

while(i <= 5) {

myArray.push(i);

i--;

};

I actually meant this version of it.

Do you see a problem here?

var i = 5;

console.log(i <= 5); // true

i--
console.log(i); // 4
console.log(i <= 5);  // true

i--
console.log(i); // 3
console.log(i <= 5);  // true

i--
console.log(i); // 2
console.log(i <= 5);  // true

Honestly I don’t because in my mind it should start at 5 and stop when it gets to 0 like it should do.

But i’m obviously wrong for some reason and I still don’t know why.

Why should it stop at 0? It stops when i<=5 is no longer true…but 0<=5 is true, -1<=5 is true, -2<=5 is true… on and on forever

Oh I got it now, thanks, I was not thinking about negative numbers at all, I thought its impossible to go beyond 0.

1 Like

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