0

I need an explanation for Javascript (For loop)

k=6; for(var i = 0; i<k; i++) { k+=0.5; } alert (i); // the answer is 12. i cant seem to understand why, with the "0.5" added to the loop. Please i need someone to explain it succintly for me.

23rd Apr 2020, 10:25 AM
Etido Elijah
Etido Elijah - avatar
7 Respostas
+ 3
If you begin with zero, how many times do you have to repeat adding 0.5 until you get to 6?
23rd Apr 2020, 11:07 AM
Ipang
+ 2
The for-loop has to be executed first? before what?
23rd Apr 2020, 3:17 PM
Ipang
+ 2
This might add to your understanding of a for/loop. https://code.sololearn.com/Ws1oc88oPZWL/#js
23rd Apr 2020, 7:48 PM
ODLNT
ODLNT - avatar
+ 1
12 times... I figured that out but felt its an oddity, Since the "for loop" has to be executed first because before the code block.
23rd Apr 2020, 11:29 AM
Etido Elijah
Etido Elijah - avatar
+ 1
I meant the statements in the () will be executed first before the statement in {}... Thanks for your response. I gained more insight in the for loop.
23rd Apr 2020, 4:06 PM
Etido Elijah
Etido Elijah - avatar
+ 1
You are welcome.
23rd Apr 2020, 8:32 PM
ODLNT
ODLNT - avatar
0
Thank you so much for that code explanation... 😎😎😎.... I got that "light-bulb" moment from it... 👌
23rd Apr 2020, 8:18 PM
Etido Elijah
Etido Elijah - avatar