+ 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?

17th Nov 2020, 5:10 PM
Francis Woli
Francis Woli - avatar
4 Answers
+ 4
Please show your code here using code playground https://www.sololearn.com/post/75089/?ref=app
17th Nov 2020, 5:18 PM
Gordon
Gordon - avatar
+ 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.
17th Nov 2020, 5:21 PM
The future is now thanks to science
The future is now thanks to science - avatar
+ 1
where from the 11?
17th Nov 2020, 5:52 PM
Francis Woli
Francis Woli - avatar
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
17th Nov 2020, 6:17 PM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar