Everything be true problem

I am having trouble understanding isNaN function. I wrote some code that seems to be working,but the part where I have the isNaN function as an if statement does not work the way I want it to. And I am really not sure if the code is just assuming that the isNaN(collection[i][pre]) part sees the collection[i][pre] as something that is just not a Number,and not something that returns a NaN. Not really sure if what I wrote actually makes sense :slight_smile: . Anyway,what seems to be the problem here and is there another way for me to describe the part of the code concerning falsy values?

function truthCheck(collection, pre) {
  var counter = 0;
  var special = 0;
  for (var i = 0; i < collection.length; i++) {
      if (collection[i][pre] === null || collection[i][pre] === 0 || collection[i][pre] === undefined || collection[i][pre] === "" || isNaN(collection[i][pre]) ) {return false;}  
else {
       if (collection[i].hasOwnProperty(pre)) {counter += 0;}
       else {counter += 1;}
     }
}
  if (counter === 0) {return true;}
  
  return false;
}

truthCheck([{"single": "yes"}], "single"); // it returns false

The plain isNaN function returns true if you passed it anything that’s literally not a number (even strings). You want the stricter Number.isNaN function, which returns true only if you passed it an actual NaN value.

Alternatively,

you don’t have to explicitly check for falsiness of a value. To check if a value is falsy, you can use if (!value)

1 Like