JS question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

JS question

Hello guys, can someone explain this code, please? Thanks a lot. var b = 0; for(var i = 0; i <= 2; i++) { for (var j = 0; j <=3; j++) { b++; } } alert(b); Output is 12.

22nd Aug 2019, 6:17 PM
David
David - avatar
2 Answers
+ 2
The inner loop runs 4 times, the outer loop 3 times. So the inner loop will be executed 3 times. This means, b is increased 3*4 times.
22nd Aug 2019, 6:24 PM
HonFu
HonFu - avatar
+ 1
Loop in loop => first 4 steps in the inner loop increment b => 4, then 3 times in the outer loop repeating the inner loop => 3 * 4 => 12.
22nd Aug 2019, 6:27 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar