Could someone explain me how this code works line to line? Thanks. | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

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

var count=0; for(var i=0;i<3;i++){ count--; for(var j=0;j<=2;j++){ count++; } } alert(count);

26th May 2018, 5:32 PM
Rastislav Romanec
Rastislav Romanec - avatar
2 ответов
+ 2
You have three variables, all three are set to zero. For the first for loop if i is less than 3, then it will subtract 1 from count. Then it will add 1 to variable i. Your next for loop does similar above, except if J is equal or less than 2 then it will add +1 to count. Then, it will add +1 to j. The alert is outside of the for loop. This means that is not part of it and will execute as normal as if the fors didn't exist. alert looks like a pop-up but I don't see windows(), so msg. It will show what count variable integer is. The fors will execute until they meet their conditional (which is the second segment of the code (>,<,<=,>=,=!, etc).
26th May 2018, 5:39 PM
Apple Blossom
Apple Blossom - avatar
0
for i =0 inner for loop run 3 time for i =1 inner for loop run 3 time for i =2 inner for loop run 3 time for i =0 count =-1 then for inner loop j=0,1,2 count=-1+3=2 then for i =1 count =2-1=1 then for inner loop j=0,1,2 count=1+3=4 then for i =2 count =4-1=3 then for inner loop j=0,1,2 count=3+3=6 so output alert as 6
26th May 2018, 5:49 PM
MsJ
MsJ - avatar