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

Ask about JS nested for loop

Can you tell me why the "outer for loop" didn't loop even when "i=0" is written inside the "for()" (result count=3) but it looped when "j=0" is written inside the "inner for loop" (result count =9) in the following code? https://code.sololearn.com/WifKPaeY77K5/?ref=app

28th Dec 2017, 10:31 AM
Muhammad Wahyudin
Muhammad Wahyudin - avatar
3 Answers
+ 1
the problem is with j variable.. u have initialized j=0 out of the loop.. so for i=0 run j values are 0,1,2 that's fine. now for second run i=1, the value of j is not reinitialised to 0 since the j=0 is out of the loop... and it finds that the value of j is 3 and terminates the loop..
28th Dec 2017, 10:41 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 1
Man, i didn't realize that.. Thanks for the explanation 👍
28th Dec 2017, 10:49 AM
Muhammad Wahyudin
Muhammad Wahyudin - avatar
0
np ;)
28th Dec 2017, 10:50 AM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar