Everything be true intermediate algorithm

hi guys i can’t seem to solve this challenge completely. i have 3 requirements unticked and i can’t seem to diagnose the problem. help please!

function truthCheck(collection, pre) {
  // Is everyone being true?
  for ( let i = 0; i < collection.length; i++) {
  let allProperties =  Object.keys(collection[i])

for (let property in allProperties) {

if ((!collection[i].hasOwnProperty(pre)) || allProperties[pre] !== true ) {
 return false;
}

}

  }

  return true;
}

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

Why are you checking allProperties[pre] !== true in your if statement below:

      if ((!collection[i].hasOwnProperty(pre)) || allProperties[pre] !== true) {
        return false;
      }

You are supposed to checking if the collection[i][pre] is “truthy” not the actual value true. You might want to review the challenge description again to make sure you understand what “truthy” means.

Also, you don’t care about all the properties in each object. You only care about the value of the property with the same name as the 2nd argument passed to the function.

Using the test case below, you are supposed to make sure that all of the values for objects’ properties with name “sex” are “truthy”.

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

In the first object {"user": "Tinky-Winky", "sex": "male"}, the value of the “sex” property is “male”. Non-blank strings are truthy, so the first object meets the requirement. In fact, in the other 3 objects, the values for the “sex” property are all non-blank strings, so they are also truthy. This means, you should return the value true.

ooo thank you i solved it… i’ve taken out what i didn’t need to use as well :grinning::smile:

function truthCheck(collection, pre) {
  // Is everyone being true?
  for ( let i = 0; i < collection.length; i++) {

if ((!collection[i].hasOwnProperty(pre)) || !collection[i][pre] ) {
 return false;
 
}

  }

  return true;
}

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

Do you really need this above?

mhm let me think… so this alone checks if its truthy --> !collection[i][pre]

It doesn’t check only if its value is truthy, it also checks if the “pre” property is existent? because if “pre” is existent then its considered truthy? correct if im wrong because its able to substitute “.hasOwnProperty”

If the property does not exist, then [collection[i][pre] would return undefined and undefined is not truthy.

In the particular case, it replaces the need for .hasOwnProperty, but you can not always do this as a substitute for .hasOwnProperty.

oh yeah that’s a good way to put it lol i can’t seem to think of it that way