Falsy Bouncer - null issue

Tell us what’s happening:
I came up with a code that works for every option but sometimes leaves a null element in the array. Could someone please look at the code and tell me why this is? (I know it can be done easier but I am more curious why it doesn’t work like this)
Thank you

Your code so far


function bouncer(arr) {
  const arrLength = arr.length;
   for (let i = 0; i <= arr.length; i++) {
   if (Boolean(arr[i]) == false) {
     arr.splice([i], 1);  
     i = 0;
  } 
  

   }
  console.log(arr);
   return arr;
}

bouncer([null, null]);

Your browser information:

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

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

Have you considered array.filter()?

But that is why I put “i = 0” to reset the counter…

It does remove the NaN, I have tried it.

Input: 0 1 2 false 4 NaN
Output: 1 2 4

so the only scenario it fails is if the all the array is falsy values?

if yes, maybe you need to check if this ‘i’ is the last value in the array. If yes, break out of the loop after you splice.

Exactly so, weird, I know

Interestingly enough, this works but only if every value in the array is falsy

sending a msg so as not to spoil the final solution

not sure what you’re trying to do here but this line “for (let i = 0; i <= arr.length; i++) {” will be trying to access an element that is not in your array