Everything Be True - Final test fails

Tell us what’s happening:
This is so annoying, I think I have nailed this code, it passes for every single test except for the last one for some reason. Is there a glitch, as it doesn’t seem to recognise the NaN.

Your code so far


function truthCheck(collection, pre) {
  // Is everyone being true?

var truthy = [false, 0, "", null, undefined, NaN]

  for (var i = 0; i< collection.length; i++){
    for (var j = 0; j < truthy.length; j++){
        if (collection[i][pre] == truthy[j]){
      return false;
    }
   
}

  }
  return true;


}

truthCheck([{"single": "double"}, {"single": NaN}], "single")

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/everything-be-true

NaN is an odd one. When you test for NaN the way you are doing it (e.g. NaN === NaN) you always get false. NaN doesn’t equal anything including itself! :smiley: When testing for NaN use Number.isNaN(number). But there’s an easier way to express your if-condition and eliminate that inner for-loop, can you think of it?

This is a common misunderstanding with the Falsy Bouncer. You can make a solution for this challenge which tests against a hardcoded list of falsy values, but it misses the point of the exercise. The concept of values being “falsy” isn’t something made up by Free Code Camp for the purposes of this challenge, but rather a quality that values have in programming languages. A value is falsy if the language treats the value as false when it is evaluated as a boolean. This means that Boolean(someFalsyValue) is false but also that we can use it in a conditional like if (someFalsyValue) . We can also talk about values being “truthy”. As I’m sure you’ve guessed, truthy values evaluate to true in a boolean of logical context.

 if (someFalsyValue) {
    // this code block is not entered
}
else if (someTruthyValue) {
    // this code block is entered and executed
}
>```

I was thinking something like Boolean(collection[pre]) but still can’t figure it out… :confused:

Dw, finally got a solution!

SPOILER ALERT

function truthCheck(collection, pre) {
// Is everyone being true?

for (var i = 0; i < collection.length; i++){
if(Boolean(collection[i][pre]) == false){
return false;
}

}

return true;

}

That works but it can be made much simpler.

  1. no need to cast to Boolean, the if-condition evaluator essentially does that.
  2. no need to do an explicit comparison with false because you can look at the if-condition evaluator as an automatic “truthy-detector”.
if(!collection[i][pre])

does the trick. Think about why this is the case. (note that the ! is necessary)