+ 4

Need explanation in this code's line of a for loop: arr.reverse([i]); Why this output?

const num = [1, 2, 3, 4, 4, 3, 2, 1]; var len = num.length; var arr = new Array(); for(let i = 0; i < len; i++) { if(arr.indexOf(num[i]) == -1) { arr.push(num[i]); arr.reverse([i]); } } console.log(arr); //Output [4, 2, 1, 3]

6th Apr 2019, 4:53 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
3 Antworten
+ 7
First iteration 1 2nd iteration 1,2 reverse it 2,1 3rd iteration 2,1,3 reverse it 3,1,2 4rd iteration 3,1,2,4 reverse it [4,2,1,3] The rest of iterations would be ignored, due to if condition is false, elements exist. please note reverse function does not has any argument.
6th Apr 2019, 5:13 AM
Calviղ
Calviղ - avatar
+ 2
Thanks Calviղ .
7th Apr 2019, 12:52 AM
Luis Febro 🇧🇷
Luis Febro 🇧🇷 - avatar
+ 1
Nice explanation
8th May 2019, 11:54 PM
Rafael S Valle
Rafael S Valle - avatar