Falsy Bouncer - Strange reaction from array.filter() and switch

Falsy Bouncer - Strange reaction from array.filter() and switch
0

#1

So when I run this code with an array containing null statement, it fails to filter out the null. What confuses me is that if I run the code with an array that only contains null statements, even multiple nulls, then the code does filter out all of the nulls. Why does this code fail? Why does it work when there are only nulls?

Your code so far

function bouncer(arr) {
  // Don't show a false ID to this bouncer.
  var newArr = arr.filter(function(i){
    switch(i){
      case(false):
      case(null):
      case(0):
      case(""):
      case(undefined):
      case(NaN):
      return false;
      default:
      return true;
    }
  });
  return newArr;
}

bouncer([1, null, NaN, 2, undefined]);```
**Your browser information:**

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

**Link to the challenge:**
https://www.freecodecamp.org/challenges/falsy-bouncer

#2

I solved my problem and the answer was as strange as I expected. The problem wasn’t the null statements; it was the NaN. It turns out NaN == NaN returns false. Isn’t that strange? So I added the Number.isNan(i) to my default case and the code worked! They should add a tip to this challenge that explains this strange effect of NaN.