function dropElements(arr, func) {
// drop them elements.
let originalLen = arr.length;
for (let i = 0; i < originalLen; i++) {
if (func(arr[0])) {
break;
} else {
arr.shift();
}
}
return arr;
}
I don’t see what makes that code work and mine not work
In cases like these it can help to just iterate through your own program with pen and paper, see if the results are as you expect. Or if you have learned how to debug in your editor, that can be helpful.
This is a fairly common issue of modifying the array you are iterating over as you go. Your i goes up, but your arr.length in your for loop also goes down, so you are iterating over only half of the element.
The problem is in the head of the for loop. With each iteration, the length of the array gets computed. But because you drop items each iteration not all elements in the array get covered. To avoid that problem the solution computes the array length first in a separate variable that never changes. I hope my explanation makes sense.