Everything be true, but why also NaN?

Hi! At the end I passed the challenge with a different, shorter code, but I can’t understand the reason why with the following code the last test case with NaN value (truthCheck([{"single": "double"}, {"single": NaN}], "single")) returns true… What am I missing?

###spoiler alert!

function truthCheck(collection, pre) {
  // Is everyone being true?
 
  for(var i in collection) {
    
    switch(collection[i][pre]){
      case null:
      case undefined:
      case 0:
      case NaN:
      case '':
      case "":
        return false;
    }
  
  }
 return true;

}

Maybe it’s because NaN is not equal to itself, so the program isn’t finding that case.

1 Like

NaN is the only JavaScript value that is not equal to itself.

var notANumber = NaN;
console.log(notANumber !== notANumber); // true
1 Like

This page helped me with NaN (and other falsey values) - https://www.sitepoint.com/javascript-truthy-falsy/

Use isNaN() instead of the case comparison.

1 Like