+ 7
Help please!!! can someone explain why the output of this code is 26?
var a = [7,9,3,4]; for (var i = 1;i<4;i++){ a[0]+=a[i]+1; } alert(a[0]);
2 Respostas
+ 11
the loops runs 3 times, with i = 1, i = 2, i = 3.
so it does:
a[0] += a[1] + 1
a[0] += a[2] + 1
a[0] += a[3] + 1
and the values are:
7 += 9 + 1 //17
17 += 3 + 1 //21
21 += 4 + 1 //26
so after the code runs a[0] is 26
+ 8
3 Iterations
[7,9,3,4]
1st Iteration i=1
● a[0] = 7
■ a[0] = a[0]+a[1]+1
= 7 + 9 + 1
= 17
2 nd Iteration i=2
● a[0] = 17
■ a[0] = a[0]+a[2]+1
= 17 + 3 + 1
= 21
3 rd Iteration i=2
● a[0] = 21
■ a[0] = a[0]+a[2]+1
= 21 + 4 + 1
a[0] = 26