+ 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]
3 Respuestas
+ 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.
+ 2
Thanks Calviղ .
+ 1
Nice explanation