Created
April 2, 2018 17:32
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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