0
For loop
Please explain to me how a is equal to 4. var a=1; for(i=0;i<=5;i++){ i+=2; a*=2; } console.log(a). //4
2 Respostas
+ 3
a = 1
loop start with i = 0
1st step:
i += 2 <=> i + 2 = 0 + 2 = 2
a *= 2 <=> a * 2 = 1 * 2 = 2
i++ <=> i = i + 1 = 2 + 1 = 3
i <= 5 is true so 2nd step:
i += 2 <=> i + 2 = 3 + 2 = 5
a *= 2 <=> a * 2 = 2 * 2 = 4
i++ <=> i = i + 1 = 5 + 1 = 6
i <= 5 is false so loop stop
at end, a = 4
+ 1
for loop will execute twice only cuz you increment i by 3 every time, so
i = 0(+2) = 2 and a = 2*a = 2 and i=i+1=3
i = 3(+2) = 5 and a = 2*a = 4 and i=i+1= 6 so loop condition becomes false and it ends