Can you explain how the result comes to be 11 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Can you explain how the result comes to be 11

var i=2; var k=1; var j=0; for(k=0;k<10;k++) j++; for(i=0;i<10;i++) continue ; j++; alert(j);

16th Oct 2017, 7:03 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
5 Answers
+ 5
got ya now. when k=0 j gets incremented and becomes j=1....so forth until the 10th iteration where in fact k happens to be 9. crystal clear. ...often times I get lost inside loops lol. thanks
16th Oct 2017, 8:32 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
+ 4
var i=2; var k=1; var j=0; // This loop gets j to 10 for(k=0;k<10;k++) { j++; } // This loop does nothing but skipping for(i=0;i<10;i++) { continue ; } // Add 1 to j, which was equal to 10 j++; // Show 11 in the alert alert(j);
16th Oct 2017, 7:07 PM
deFault
+ 4
thank you very much for your clarifications. still , I thought setting the first loop would restrict the incremention of j to 9 and not 10 . could you elaborate on that please.
16th Oct 2017, 8:08 PM
Abdoulaye Seck
Abdoulaye Seck - avatar
+ 2
Well, because k is now equal to 0 in the first loop, there are 10 numbers to iterate through: [0, 1, 2, 3, 4, 5, 6, 7 ,8, 9].
16th Oct 2017, 8:24 PM
LunarCoffee
LunarCoffee - avatar
+ 1
j starts from 0 and gets incremented with each loop cycle. The loop will run 10 cycles: k=0, k=1, k=2, k=3, k=4, k=5, k=6, k=7, k=8, k=9. k=10 would've been 11th cycle. We start from 0, end at 9 (inclusive), so including 0 and 9, we get 10 cycles. for(k=1;k<=10;k++) {...} is also 10 cycles.
16th Oct 2017, 8:25 PM
deFault