Intermediate Algorithm Scripting: Everything Be True Problem

Hello,

First time poster so please let me know how I can make this a better post. I am unsure why the condition el[e] === NaN is returning true. All of the other conditions pass except this one.

function truthCheck(collection, pre) {
    let verify = true;
    collection.forEach(el => {
      console.log(el);
      for(let e in el) {
        console.log(`${e}: ${el[e]}`);
        if(e === pre && el[e] === 0 || el[e] === null || el[e] === undefined || el[e] === ""|| el[e] === NaN) {
          console.log('Falsy');
          verify = false;
        } 
        if(!el.hasOwnProperty(pre)) {
          verify = false;
        }
      }    
    });

    console.log(verify);
    return verify;
  }

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

By this, I take it you mean your logic is saying the value is true, not that the condition is returning true. NaN is never equal to anything else, not even NaN. To check whether something is NaN, you need Number.isNaN(val)

2 Likes

You know a think you can do with falsy and truthy values?

!0 // this evaluates to true
!!0 // false
!1  // false
!!1  // true
!undefined    // true
!!undefined    // false

Maybe this can be useful for you

Thank you. This helped and now works!!! Much appreciated