+ 2
How does it work?
Uhhh... how can I be so stup*d ^_^... Can you please guys help me... how does this code work????? var arr=[4,5,3,2] for(var i=0; i<arr.length; i++){ arr[i]*=i; } alert(arr[i/2]); result: 6 Thanks for your answer...
2 Answers
+ 10
For each variable in arr, the variable is multiplied by the array index.
Resulting arr = [0, 5, 6, 6]
alert(arr[i/2]) alerts the element at index i/2 in arr, which is the 3rd element (index 2), 6.
+ 1
After for loop values of array are:
0 <- 4*0
5. <- 5*1
6. <- 3*2
6. <- 2*3
i equals 4 now, so arr[4/2] = arr[2] = 6
Hope it helped!