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
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.
for ([initialExpression]; [conditionExpression]; [incrementExpression])
statement
And they work as follow.
initial expression is evaluated
condition expression is evaluated. if true run the statement
the statement execute
the increment expression is executed.
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
You can read all about loops here
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.
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.
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.