Everything Be True question

Everything Be True question
0

#1

Tell us what’s happening:
On the last if statement, when I change the conditions around(falseCount===0 and falseCount>0), the completed Ii had become grey and the other one are ticked. Why is that, if the statement still covers both conditions?

Your code so far


function truthCheck(collection, pre) {
 for (let i = 0; i < collection.length; i++) {
   for (let key in collection[i]) {
   var falseCount=0;
     if (collection[i].hasOwnProperty(key)) {
       console.log("checking " + JSON.stringify(collection[i]));
       if (collection[i][pre]) {
         console.log("this is true!");
       } else {
         console.log("this is false!");
         falseCount++;
         console.log("falseCount is " + falseCount);
        }
      }
      
      }
      if (falseCount=0) {
        return true;
    }   else {
      return false;
    }
  }
}

truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");

Your browser information:

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

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


#2

The code you posted above only checks the first object in the array, before returning a true or false value. The following if statement is actually assigning a value of 0 to falseCount and then evaluating the if statement as if (0) {, so the else block of code will always execute. You should be comparing falseCount to zero with the === operator if you want to return true when falseCount is zero.

    if (falseCount = 0) {
      return true;
    } else {
      return false;
    }

I do not exactly understand what code you tried, so if you want to post the exact if statement you tried, then I can see what you meant in the above question.


#3

Nevermind, I figured out that specific statement I believe. I moved the if statement until after the for loop. Now, I think that if falseCount is anything other than zero, it should return false. But from the console, I can’t really see why e few of them return the wrong thing. They also appear twice for some reason. Here is my code:

function truthCheck(collection, pre) {
 for (let i = 0; i < collection.length; i++) {
   for (let key in collection[i]) {
   var falseCount=0;
     if (collection[i].hasOwnProperty(key)) {
       console.log("checking " + JSON.stringify(collection[i]));
       if (collection[i][pre]) {
         console.log("this is true!");
       } else {
         console.log("this is false!");
         falseCount++;
         console.log("falseCount is " + falseCount);
        } else if (!collection[i].hasOwnProperty(pre)) {
          return false;
        }
      }
      
      }
     
    }
    if (falseCount===0) {
        return true;
    }   else {
      return false;
  }
  }
   


truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");