0
What will be answer?
var arr=[1,4,6,4,2]; for (var i = 0; i < 3; i++) { arr[i]+=arr[i+2] } alert(arr[0])
10 Respuestas
+ 2
We are only interested in the first iteration (i=0). So we have:
arr[0] += arr [0+2]
This is equivalent to:
arr[0] = arr[0] + arr[0+2]
arr[0] is 1, arr[2] is 6, so we have:
arr[0] = 1 + 6
arr[0] = 7
+ 8
arr[0] will be added with arr[2], hence the answer is 7.
+ 6
a [i]=a [0]=1
a [i+2]=a [0+2]=a [2]=6
a [i]+=a [i+2]
a [0]=a [0]+a [2]=1+6=7
I hope this helps @Kristina
If you have further doubts, feel free to ask
+ 5
The loop takes i from 0 to 2.
In the first iteration i equals 0, so the instruction goes:
a[0] += a[0+2], so it adds a[2] to a[0] (6 to 1, which gives 7)
Later, the loop goes through two other array elements, but in the end you just print a[0] which is equal to 7
+ 5
If you are using Chrome under Windows just select code, ctrl-c, ctrl-shift-i, ctrl-v, enter
+ 4
arr[0] = 1
in the loop when i = 0
arr[i] (arr[0] which is 1)+= arr[i + 2] (arr[0 + 2] which is 6)
+ 3
7
+ 1
var arr=[1,4,6,4,2];
for (var i = 0; i < 3; i++) {
arr[i]+=arr[i+2]
}
console.log(arr); // 7,8,8,4,2
Since arr[i]=arr[i]+arr[i+2]
for i=0
arr[0]=arr[0]+arr[2]; //arr[0]=1 arr[2]=6
arr[0]=1+6;
arr[0]=7;
- 1
don't understand how can added
- 1
but here I +2 6+2