Explanation of the result of this challenge | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Explanation of the result of this challenge

var count = 0; for (var i = 0; i < 3; i++) { count--; for (var j = 0; j < 3; j++) { count++ } } console.log(count);//returns 6

1st May 2020, 3:28 PM
Sajid
Sajid - avatar
4 Answers
+ 4
count=0 values of i =[0,1,2] values of j=[0,1,2] first for loop value I=0 count-- First value = -1 Second for loop count++ 3 times -1+1+1+1=2 I=1 count-- -> 2-1 = 1 count++ 3 times 1+1+1+1 = 4 i=2 count-- -> 3 count++ 3times 3+1+1+1 = 6
1st May 2020, 3:37 PM
Justus
Justus - avatar
+ 3
For each i to the count will be j times 1 added, that means 3*3 = 9. for each i will be from count 1 subtracted, that means 3 times. 9-3 = 6.
1st May 2020, 3:46 PM
JaScript
JaScript - avatar
+ 1
Bcos the second for loop is in the first. For every iter second for the loop is executed. I.e Everytime the first for loop runs, The second loop is executed 3 times
1st May 2020, 3:49 PM
Justus
Justus - avatar
0
Justus what do you mean by count++ 3 times? According to me, it should return 0
1st May 2020, 3:44 PM
Sajid
Sajid - avatar