Last active
February 20, 2019 03:14
-
-
Save JeffOgah/3ca76b0469b81682ae5901a6440d50b9 to your computer and use it in GitHub Desktop.
JavaScript Intermediate Algorithm Scripting: Everything Be True
This file contains hidden or 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
| /* Check if the predicate (second argument) is truthy on all elements of a collection (first argument). | |
| In other words, you are given an array collection of objects. | |
| The predicate pre will be an object property and you need to return true if its value is truthy. Otherwise, return false. | |
| In JavaScript, truthy values are values that translate to true when evaluated in a Boolean context. | |
| */ | |
| function truthCheck(collection, pre) { | |
| // Is everyone being true? | |
| for (let obj in collection) { | |
| if (!collection[obj][pre]) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| 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