Js loop problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Js loop problem

how result is 25 when loop cant be greater than 24 var i =1; for(; i < 24; i+=3){ } console.log(i) //25

4th Jun 2019, 2:18 AM
Danielov
Danielov - avatar
3 Answers
+ 3
because 22 is less than 24 so when it adds 3 it automatically goes to 25. It goes by 3's, but it wont refrain from skipping over the maximum- itll just stop after it has gone over it
4th Jun 2019, 2:20 AM
koala 🐨
koala 🐨 - avatar
+ 3
Remember that for every iteration of the loop, (except the first of course) the counter variable ( in this case var i) is incremented first before checking the condition. So after the 7th iteration, i becomes 22. Then for the next iteration, 3 is added to make it 25 then the condition is checked which now evaluates to false.
4th Jun 2019, 2:26 AM
Jonathan Pizarra (JS Challenger)
Jonathan Pizarra (JS Challenger) - avatar
0
According to the condition (i<24) the loop should terminate when i becomes greater than 24 That is i = 25 which is according to the incrementation
4th Jun 2019, 1:03 PM
Tony Stark
Tony Stark - avatar