JavaScript question

So apparently the correct answers is 3. I don’t get why there are 3 loops before the code stops. In the 3rd loop i = 2 and x = 3. Shouldn’t there be 1 more loop since i < 3 . I get why x = 3 what I don’t get is my the loops stops there at i = 2 and x = 3 . i still smaller than 3
No description
14 Replies
briancross
briancross15mo ago
The loop will execute as long as x is less than 3 so when it’s 0, 1, and 2.
ErickO
ErickO15mo ago
and x = 3 . i still smaller than 3
is it tho? think for a moment, is 3 lower than 3? does 3 come before 3? if you add 1 to 3 is it now equal?
Wolle
Wolle15mo ago
For-loops evaluate the expression before executing. So it gets executed for 0, 1 and 2 and stops the moment i is 3.
Shivam Goswami
Shivam Goswami15mo ago
It runs 1 and 2 only Also 0
Solo Incrementing
As Wolle said, the condition is evaluated before a for loop iteration is run. In the beginning, i is set to 0, checks if i < 3, it is, so runs, then adds 1 to it. Then it runs again for i=1 because 1 < 3. Again for i=2 because 2 < 3. On that "4th loop" i = 3, but 3 < 3 is false, so it the code inside isn't run. In the last run iteration of the loop, i=2. In total there was 3 iterations. i=0,i=1,i=2. Therefore 1 was added to x, 3 times, resulting in x=3. Hope I clarified how the for loop works here properly :)
Oxycodone
OxycodoneOP15mo ago
The loop goes like this First i = 0 x = 1 2nd i = 1 x = 2 3rd i = 2 x = 3 Why the code doesn’t start here a 4th time since i < 3. This is the part that gets me
Solo Incrementing
Ya, oh my bad you're adding i to x each time, well the answer is still x + 0 + 1 + 2 = 3 3 < 3 evaluates to false because 3 is not less than 3, its equal, if it was 3 <= 3 then it would be true and run that 4th time
Oxycodone
OxycodoneOP15mo ago
So the loop calculates what i becomes before starting the loop?
Solo Incrementing
Well actually the order is: 1. on first run: Set initial i value, check condition (so i=0, 0 < 3 so run the code) 2. run the code if the condition was true 3. increment then repeat 2 and 3 The increment happens after an iteration is run if I remember correctly
Jochem
Jochem15mo ago
this is correct. More accurately the third part of a for loop's definition runs at the end of each loop, it doesn't have to be an increment
Oxycodone
OxycodoneOP15mo ago
If this said, in 3rd loop we all agree i = 2 but… 4th loop : for( i = 2 , 2<3, 2+1) X= 2 + 3 x = 5 i becomes 3 after the condition is met, not before the condition
Jochem
Jochem15mo ago
The first iteration i is 0, which satisfies 0 < 3 so the codeblock is run. i is added to x which is now also 0. Then i increments and is 1. The second iteration, i is 1, which satisfied 1 < 3, so the codeblock is run. i is added to x which is now 1. Then i increments and is 2. The third iteration, i is 2, which satisfied 2 < 3, so the codeblock is run. i is added to x which is now 3. Then i increments and is 3. The fourth iteration, i is 3, which DOES NOT satisfy 3 < 3, so the codeblock is NOT run. Code execution continues below the for loop's codeblock. i is NOT added to x because the condition wasn't met and the codeblock was not run. The next line contains a console.log which is executed and outputs 3 the contents of the for loop's brackets aren't executed in order before the for loop's codeblock runs, each part is executed at different times during the execution of the entire block. The first part is only run once. The second (the comparison) executes once at the start of each iteration of the loop. If it returns false, the loop is not run at all and execution continues after the for loop's codeblock The third part (in this case the increment of i) executes once at the end of each iteration of the loop, just before the next validation of the second part
Oxycodone
OxycodoneOP15mo ago
Thank you for spending time to write that much! ❤️
Jochem
Jochem15mo ago
no worries 🙂 Do you understand what's going on now?
Want results from more Discord servers?
Add your server