Since "5 "is less than "6", why would it not add "1 "

Hi Campers,
I hope this is not a silly question, about the for loop.

In the code below, I expect the condition (i < 6) also aply when I reach number “5”.
Since "5 "is less than “6”, why would it not add "1 " and the end number be “6” instead of "5 "?

Thanks.

Your code so far


// Setup
const myArray = [];

// Only change code below this line
for (let i = 1; i < 6; i++)
{myArray.push(i);}
console.log(myArray);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.2 Safari/605.1.15

Challenge: Iterate with JavaScript For Loops

Link to the challenge:

Took me a moment to understand the question…
The “i++” happens AFTER the body of the loop is executed - hence in the last loop i is 5.
It’s the same reason as to why the first number is 1 and not 2.

Thanks Jagaya,

I understand that “1” is the initialization, so would be the first number.

According to the condition statement we add +1 to the initialization number, so:

    Initialization = 1     (which I guess is before the body of the loop is executed )
                      1 + 1  = 2
                      2 + 1  = 3
                      3 + 1 = 4
                      4 + 1 = 5

5 (is still < 6) + 1 = 6

I don´t understand why the body of the loop stops before the last number that complies to the condition statement, since 5 < 6.

Sorry I am being silly here, but I don´t get why 5 would not be considered for the condition statement and the final number be 6.

for loop has the following syntax

for ([initialExpression]; [conditionExpression]; [incrementExpression])
  statement

And they work as follow.

  1. initial expression is evaluated
  2. condition expression is evaluated. if true run the statement
  3. the statement execute
  4. the increment expression is executed.
  5. back at step 2

It’s important to remember than that the increment is done after the body executes.
This means that in your case

i = 1
i < 6 ? true --> add 1 in the array.
i++ -> i = 2

[...same for 2,3,4]

i = 5
i < 6 ? true --> add 5 in the array.
i++ -> i = 6

i = 6
i < 6 ? false -> exit the loop

Meaning that the expression (adding inside the array) is never run.

Hope this helps clarify things :sparkles:
You can read all about loops here

1 Like

Just to clarify, are you wondering why your output is not [ 1, 2, 3, 4, 5 ] or why it is not [ 1, 2, 3, 4, 5, 6 ]?

I see,

So the answer has to comply to the condition statement as well ?
Otherwise it would considered false.

It was quite helpfull Marmiz.
Many thanks to the team.

Hi SwedishErik

My question was why it is not: [ 1, 2, 3, 4, 5, 6 ]
Since the condition statement (i < 6) would be true for number “5”.

But Marmiz explained that the answer has to comply with the condition statement as well, so

Many thanks

I am not really sure what you means with this.
Answer?

Don’t just focus on getting the correct answer to this test, but to rather understand how the for loop execute, and how you could use it to your advantage in all possible scenarios. :smile:

Yes, I am trying to understand how the for loop works.

When:
i = 4
i < 6 ? true
i++ = 5 → That´s OK.

But when:
i = 5
i < 6 ? → Is also true
So I expected → i++ = 6
The result here is 6.

But from your previous post I think I understood that the result of
i++ =
has to comply with the condition statement as well.
So, when: i = 5
i < 6 ? → Is also true
But i++ = will have a result of “6”, which then

Ok, great to hear it make sense now. Yeah, loops are tricky. Sometimes they are doing their job and it is all nice and fun. Sometimes they make you a little dizzy, other times they make you literally loopy and sometimes you accidentally make an infinite lo- OH NO! STOP! STOP! What is the stop command in this program?! I can’t remember the stop command!! My computer is melting from overheating!!

Hopefully you will know them best by the first alternative and not the last.

Sure, it seems easy when you understand it, hehe

It is a bit confusing to me that “i” has different values,
one value I will consider to see if the condition statement aplies:
i = 5
i < 6 → True

But then, when I execute (5 + 1)
i++ = 6
And then
i < 6 → False

I think I understood now that the condition statement has to be complied by the final expresion in order to be True.

So, in this case, the final number is not included because does not comply with the condition statement.

Many thanks for your patience.

I think that sounds right. Worth to remember that in loops you can also also write an “lesser than or equal to”-sign instead of using only a “lesser than”-sign for your condition statement. In this case that would make it include the number 6 in the output too, perhaps that agrees better with your intuitive view of it.

You need to set up like this for (let i = 1; i =< 6; i++). You are requesting an answer that is less than 6, =< when you want it to stop at 6 so it has to be also equal to 6.