Question on Everything Be True

Hello. I can’t seem to figure out how come in my algorithm the if statement passes everything as true. Can someone please help me with this code?


function truthCheck(collection, pre) {
var truthCount = 0;
  
  
  for(var i = 0; i<collection.length; i++) {
    if (collection[i].hasOwnProperty(pre) === true) {
      truthCount +=1;
    }
  }
  if (truthCount == collection.length) {
    return true;
  } else {
    return false;
  }
}
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age");

no matter what the argument that’s passed in, truthCount always ends up equalling collection.length.

Your usage of collection[i].hasOwnProperty(pre) tests if the element has the same property. Your code returns true for all 9 examples, as their elements all have the same property as pre.

What you should be testing for is the value of the properties. The challenge gives you this hint:

Remember, you can access object properties through either dot notation or notation.

But isn’t the function supposed to test if it has the same property as pre?
I’m confused as to what my code is doing that it shouldn’t be.

You want to check if every property pre in your collection holds a truthy value.

1 Like