So I´m trying to do the challenge https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/drop-it and I´m doing some tests.
By now I´m trying to see if i can make it work by putting manually the condition, here´s what I´m doing:
function dropElements(arr, func) {
for(number in arr){
if (arr[number] === 1){
return arr
}
else{
arr.shift()
}
}
return arr
}
dropElements([0, 1, 0, 1], function(n) {return n === 1;})
Please explain me this:
In the first iteration, arr[0] is equal to 0, so When the engine reaches the line arr[number] === 1
that is not true, so goes to the else statement and does arr.shift()
So here it comes what i dont understand, in the second iteration, arr[1] is equal to 1, so When the engine reaches the line arr[number] === 1
that is true. So it just should return arr (which at that point is[1,0,1]
) because in the first iteration JS did the arr.shift()
line
However, it doesn´t return that, it returns [0,1]
please enlighten me why