+ 3

Could someone please explain?

I know the answer is 12 but I don’t understand why: var a=0; for (k=1; k<=2; k++) { for (i=0; i<=5; i++) {} a++; console.log (a); }

17th Apr 2018, 8:13 PM
Levi
Levi - avatar
6 Answers
+ 3
The answer is 12 because you display the value of a variable each iteration so at first iteration a is 1 so it displays 1 on console then at the second iteration a is increased by 1 again so it displays 2 beside 1
17th Apr 2018, 8:25 PM
TurtleShell
TurtleShell - avatar
+ 2
Thanks @turtleshell. So the second loop isn’t factored?
17th Apr 2018, 8:28 PM
Levi
Levi - avatar
+ 2
The second loop is useless
17th Apr 2018, 8:29 PM
TurtleShell
TurtleShell - avatar
+ 1
Why is that may I ask?
17th Apr 2018, 9:19 PM
Levi
Levi - avatar
+ 1
Ahhh... so obvious. Thank you!
17th Apr 2018, 10:16 PM
Levi
Levi - avatar
0
Theres nothing inside the loop’s brackets for (i=0; i<=5; i++) {} <—
17th Apr 2018, 9:20 PM
TurtleShell
TurtleShell - avatar