Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Check if the predicate (second argument) is truthy on all elements of a collection (first argument). Remember, you can access object properties through either dot notation or [] notation.
function truthCheck(collection, pre) {
// Is everyone being true?
//set a variable to count the truthies
var truthy = 0;
//loop over the array and test for true
//if it's true add one to truth
for(var i = 0; i<collection.length; i++){
if(collection[i][pre]){
truthy ++;
}
}
//check if everything is true by comparing truthy to the array length.
return (truthy === collection.length);
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment