Hi guys i am wondering why the output is 7 can anyone explain | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 1

Hi guys i am wondering why the output is 7 can anyone explain

https://code.sololearn.com/W4jC35g83qvl/?ref=app

10th Sep 2019, 7:53 PM
sayed mohamed
3 Respostas
+ 7
we know that arr[0]=1 and arr[2]=6 In the for loop you did this: arr[0]+=arr[2] So now arr[0] is equal to 7 and then you print that and get 7. edit: THANK YOU VERY MUCH FOR THE LIKES
10th Sep 2019, 7:58 PM
KfirWe
KfirWe - avatar
+ 3
var arr=[1,4,6,2]; for (var i=0;i<3;i++){ arr[i]+=arr[i+2]; } console.log(arr[0]); in each iteration of the loop you add the (n+2)th element to the (n)th element of the array so after the loop the array looks like this: [1,4,6,2] [7,6,NaN, 2] the NaN occurs because arr[4] is not inside the list so adding undefined to a number results in (N)ot(A)(N)umber.
10th Sep 2019, 7:58 PM
Anton Bƶhler
Anton Bƶhler - avatar
+ 2
KfirWe thank you it is very clear now
10th Sep 2019, 8:02 PM
sayed mohamed