Drop it algorithm: I COULD google an answer, but I can't see anything wrong with my code

Drop it algorithm: I COULD google an answer, but I can't see anything wrong with my code
0.0 0

#1

Thanks in advance. I’ve changed my code up several times, and no dice. I am dropping too many items from the array. Tried moving things around. I don’t see anything significantly different from online solutions. What am I doing wrong?

function dropElements(arr, func) {
var testArr = [];
for(var i=0; i<arr.length; i++){
var temp = func(arr[i]);
if(temp){break;}
else{arr.shift();}
}
// Drop them elements.
return arr;
}

dropElements([0, 1, 0, 1], function(n) {return n === 1;});


#2

The main problem is with arr.shift (); Shift removes the first element in the array, even when your loop is not looking at it. There are a number of ways to fix this, look into array.splice or array.filter or array.reduce.


#3

OK. Thanks. I will try some other things.