Need Help with falsy bouncer

Tell us what’s happening:
My code is unable to clear all cases except case 2. It seems to me that it should work properly, but I’ve no idea why it doesn’t. Thanks

Your code so far


function bouncer(arr) {
// Don't show a false ID to this bouncer.
 for(let i=0;i<arr.length;i++)
{
  if(!arr[i])
  arr.splice(i,1);
}
return arr;
}

bouncer([7, "ate", "", false, 9]);

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36.

Challenge: Falsy Bouncer

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-algorithm-scripting/falsy-bouncer

You typically don’t want to mutate an array that you are iterating over. Problems like this arise because elements are shifting as well as being removed.
If the first element in your array is falsey, then it is removed and what used to be the second element is now at index 0. But you have already checked index 0, so you don’t check that value.

Got it. Thank you :grinning:

I’m glad I could help. Happy coding!