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])

15th Jun 2017, 9:57 AM
Kristina Hakobyan
Kristina Hakobyan - avatar
10 Respostas
+ 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
15th Jun 2017, 10:50 AM
Bogdan Sass
Bogdan Sass - avatar
+ 8
arr[0] will be added with arr[2], hence the answer is 7.
15th Jun 2017, 10:00 AM
Hatsy Rei
Hatsy Rei - avatar
+ 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
15th Jun 2017, 11:09 AM
Sri Lakshmi
Sri Lakshmi - avatar
+ 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
15th Jun 2017, 10:08 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 5
If you are using Chrome under Windows just select code, ctrl-c, ctrl-shift-i, ctrl-v, enter
15th Jun 2017, 2:35 PM
SANYABEAST
SANYABEAST - avatar
+ 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)
15th Jun 2017, 10:24 AM
jay
jay - avatar
+ 3
7
15th Jun 2017, 10:00 AM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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;
16th Jun 2017, 3:44 AM
Din Esh
Din Esh - avatar
- 1
don't understand how can added
15th Jun 2017, 10:02 AM
Kristina Hakobyan
Kristina Hakobyan - avatar
- 1
but here I +2 6+2
15th Jun 2017, 10:12 AM
Kristina Hakobyan
Kristina Hakobyan - avatar