Falsy Bouncer for loop not working

I know that is not the best solution for the challenge, but why is not working? It is taking away just the first falsy element that it founds on the for loop.


function bouncer(arr) {
for (let i = 0; i < arr.length; i++) {
  if (arr[i] === false) {
    arr.splice(i, 1)
  }
  else if (arr[i] === null) {
    arr.splice(i, 1)
  }
  else if (arr[i] === 0) {
    arr.splice(i, 1)
  }
  else if (arr[i] === "") {
    arr.splice(i, 1)
  }
  else if (arr[i] === undefined) {
    arr.splice(i, 1)
  }
  else if (arr[i] === NaN) {
    arr.splice(i, 1)
  }
  
} 

return arr;
}

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

Challenge: Falsy Bouncer

Link to the challenge:

Checking for equality against some falsy values just won’t work. NaN === NaN is false.

You should instead use the idea of Falsy.

Also, splicing out elements of an array as you iterate over it will create a massive headache. You should not modify the array you are iterating over.

2 Likes

Thank you! Totally got what you mean

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.