+ 1
Hi guys i am wondering why the output is 7 can anyone explain
3 Réponses
+ 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
+ 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.
+ 2
KfirWe thank you it is very clear now