+ 2

Why 10?

var a=0; for (var i = 0; i < 3; i++) { for (var j = 3; j > 0; j--) { ++a; } } document.write(++a)

7th May 2017, 7:36 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
12 Réponses
+ 4
1. i = 0; i < 3 true; i = 0 + 1 j = 3; j > 0 true; j = 3 - 1 a = 0 + 1 j = 2; j > 0 true; j = 2 - 1 a = 1 + 1 j = 1; j > 0 true; j = 1 - 1 a = 2 + 1 j = 0; j > 0 false ..... stop loop 2. i = 1; i < 3 true; i = 1 + 1 j = 3; j > 0 true; j = 3 - 1 a = 3 + 1 j = 2; j > 0 true; j = 2 - 1 a = 4 + 1 j = 1; j > 0 true; j = 1 - 1 a = 5 + 1 j = 0; j > 0 false ..... stop loop 3. i = 2; i < 3 true; i = 2 + 1 j = 3; j > 0 true; j = 3 - 1 a = 6 + 1 j = 2; j > 0 true; j = 2 - 1 a = 7 + 1 j = 1; j > 0 true; j = 1 - 1 a = 8 + 1 j = 0; j > 0 false ..... stop loop 4. i = 3; i < 3 false ..... stop loop document.write(a++) 9 + 1 OUTPUT 10 Kristina this is my quiz on Quiz Factory
23rd Jun 2017, 6:51 AM
Didi Georgel Danaila
Didi Georgel Danaila - avatar
+ 16
1. Iteration, i=0 inner loop runs 3 times, so a=3 2. Iteration, i= 1 a=6 3. Iteration, i=2 a=9 ++a results in 10, output 10.
7th May 2017, 7:42 PM
Tashi N
Tashi N - avatar
+ 13
As @Kuba said: The first loop is called outer loop. It immediately executes the inner loop, until the inner loop ends all iterations after 3 times running. The outer loop still has 2 iterations to execute. So... it immediately runs the inner loop, which runs 3 times again. Then the outer loop runs the last iteration... and immediately runs the inner loop - 3 times. Then the outer loop is completed.
7th May 2017, 8:05 PM
Tashi N
Tashi N - avatar
+ 11
@Kuba You won the race ;)
7th May 2017, 7:44 PM
Tashi N
Tashi N - avatar
+ 10
It goes 3 iterations by 3, so 9 altogether. (++a means increase by 1) After that, you ask to increase by 1 one more time and then print. 9+1=10, so... ;)
7th May 2017, 7:39 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 10
Wrote a code, maybe it helps you... https://code.sololearn.com/WdQR42E3EecP/?ref=app
7th May 2017, 7:57 PM
Tashi N
Tashi N - avatar
+ 10
You're welcome.
7th May 2017, 8:09 PM
Tashi N
Tashi N - avatar
+ 7
Those are nested loops. One of them iterates three times and within each iteration it triggers the inner one three times (each time!). So the inner loop is run nine times altogether. And its sole instruction is ++a which means (increase a by 1). After the looping finishes, a equals 9. In the end you ask to print out ++a, which means "print out a increased by 1". So a is increased by 1 (now it equals 10) and is printed on the screen.
7th May 2017, 7:51 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 7
@Tashi N woww that was some clear cut explanation can't get any better, I usually had an ideas of nested loops this takes it further thank you.
7th May 2017, 8:04 PM
Jibin Philipose
Jibin Philipose - avatar
+ 4
thx but we have a two loop why going up
7th May 2017, 8:00 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
+ 4
thx so much
7th May 2017, 8:07 PM
Kristina Hakobyan
Kristina Hakobyan - avatar
+ 3
can't understand sorry
7th May 2017, 7:47 PM
Kristina Hakobyan
Kristina Hakobyan - avatar