0
Why this code give answer 9 ?
var count=0; for(var i=0;i<3;i++){ for(var j=0;j<=2;j++){ count++; } } Document.write(count);
2 Réponses
+ 2
Outer loop runs for i =0,1,2 ;
so 3 times iterates
*(multiply)
(Inner loop runs 3 times j = 0,1,2 so increments count 3 times.)
Hence, 3*3 =9 times count++ executes so count=9.
Is it OK now?
What is your answer?
0
Initially cout = 0
When i=0
J= 0, 1 ,2
Cout = 1
1
1
Now cout =0 + 3 = 3
When i=1
J= 0, 1 ,2
Cout = 1
1
1
Now cout = 3 + 3 = 6
When i=2
J= 0, 1 ,2
Cout = 1
1
1
Now cout = 6 + 3 = 9