Created
April 17, 2020 05:35
-
-
Save yitonghe00/36d77c8aab24bc47cb74d732eca5552d to your computer and use it in GitHub Desktop.
Eloquent JavaScript 0503 Everything.js https://eloquentjavascript.net/05_higher_order.html#i_SmbRSAd5GA
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 every(array, test) { | |
// Your code here. | |
for (let elem of array) { | |
if (!test(elem)) return false; | |
} | |
return true; | |
} | |
*/ | |
function every(array, test) { | |
// Your code here. | |
return !array.some((elem) => !test(elem)); | |
} | |
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