Skip to content

Instantly share code, notes, and snippets.

@nickihastings
Created April 2, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nickihastings/4b5edf7799cfc6b84eca6082018f15f7 to your computer and use it in GitHub Desktop.
Save nickihastings/4b5edf7799cfc6b84eca6082018f15f7 to your computer and use it in GitHub Desktop.
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