+ 4

Could someone explain me how this code works line to line? Thanks.

k=6; for(var i=0;i<k;i++){ k+=0.5; } alert(i);

3rd May 2018, 1:18 PM
Rastislav Romanec
Rastislav Romanec - avatar
4 Antworten
+ 6
Output is 6. In loop: i=0 it increases until i=5 Out of loop: i will become 6
3rd May 2018, 1:23 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
25th May 2018, 1:34 PM
r8w9
r8w9 - avatar
+ 4
Rastislav Romanec Zdar, k se ti po celém běhu sesčítá na 12. (k+=0.5). Tím pádem dostaneš for loop for (var i=0; i<12;i++){ console.log(i); // vypíše hodnoty 0 až 11 } console.log(i); //má nyní hodnotu 12, protože je mimo loop
25th May 2018, 1:33 PM
r8w9
r8w9 - avatar
+ 1
But, i have got output 12 from this code.
4th May 2018, 1:22 PM
Rastislav Romanec
Rastislav Romanec - avatar