+ 2
Javascript I need help Arrays
var arr = [1,2,3,4]; arr[arr[1]] = 5; console.log(arr) Why this code is // 1,2,5,4 Inistead of // 1,5,3,4 I need help
4 Respostas
+ 9
arr = [1,2,3,4];
|
Index 0,1,2,3
arr[ 1 ] == 2 right ?
So
arr[ arr[1] ] == arr[ 2 ]
Finally we set index 2 = 5
arr[ 2 ] = 5
We have
arr = [1,2,5,4];
|
Index 0,1,2,3
+ 7
arr[1] = 2
arr[arr[1]] = arr[2]
Finally:
arr[2] = 5
The array starts from 0. (0 is the first element
arr[0] = 1
arr[1] = 2
...
arr[3] = 4
+ 2
Thanks i get it its can a tricky 😂
0
Pedro H.J 💚 Alex Tușinean 🍏 thanks guys... Had to stare at this for awhile until it all made sense lol 👍🏽