Created
February 20, 2018 10:57
-
-
Save jonurry/15015c95c2065fea9c2b0c4b744826e9 to your computer and use it in GitHub Desktop.
5.3 Everything (Eloquent JavaScript Solutions)
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
| // 5.3 Everything | |
| //every using a loop | |
| function every(array, test) { | |
| for (let element of array) { | |
| if (test(element) === false) { | |
| return false; | |
| } | |
| } | |
| return true; | |
| } | |
| console.log(every([1, 3, 5], n => n < 10)); | |
| // → true | |
| console.log(every([2, 4, 16], n => n < 10)); | |
| // → false | |
| console.log(every([], n => n < 10)); | |
| // → true | |
| // every using array.some | |
| function every(array, test) { | |
| return array.some(test); | |
| } | |
| console.log(every([1, 3, 5], n => n < 10)); | |
| // → true | |
| console.log(every([2, 4, 16], n => n < 10)); | |
| // → false | |
| console.log(every([], n => n < 10)); | |
| // → true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I used the
somemethod withfor/ofloop, but now I understand how to usesometo confirm that all the items of the list passed the test with the help of De Morgan's law.Thanks, Christoph!