+ 2
why is the answer to this question 12? k=6; for(var i=0; i<k; i++){k+= 0.5; } document.write(i);
why is the answer 12?
4 Respuestas
+ 4
Please show your code here using code playground
https://www.sololearn.com/post/75089/?ref=app
+ 2
beacuse when i becomes 11 in for loop, k becomes 6+0.5*12=6+6=12
so the next time i (value of i is 12 now) becomes equal to k and the loop won't run. So when you print i you get 12.
+ 1
where from the 11?
0
I starts at 0, k starts at 6. In each iteration i is increased by 1 and k by 0.5, so i "catches up" to k. Loop stops when i>=k, so when i caught up to k. That is at i=12