Skip to content

Instantly share code, notes, and snippets.

@gitfaf
Last active August 17, 2017 23:13
Show Gist options
  • Save gitfaf/ce1983634a3e87216ad3ae90f717a157 to your computer and use it in GitHub Desktop.
Save gitfaf/ce1983634a3e87216ad3ae90f717a157 to your computer and use it in GitHub Desktop.
Every and Some functions
function every(array, predicate) {
let holdsTrue = true;
for (let i = 0, length = array.length; i < length && holdsTrue; i++) {
holdsTrue = predicate(array[i]);
}
return holdsTrue;
}
console.log(every([1, 2, 3, 4, 5], x=>x < 6));
console.log(every([1, 2, 3, 4, 5], x=>x > 6));
console.log(every([1, 2, 3, 4, 5], x=>x > 3));
console.log(every([1, 2, 3, 4, 5], x=>x > 0));
function some(array, predicate) {
let seenTrue = false;
for (let i = 0, length = array.length; i < length && !seenTrue; i++) {
seenTrue = predicate(array[i]);
}
return seenTrue;
}
console.log(some([1, 2, 3, 4, 5], x=>x < 6));
console.log(some([1, 2, 3, 4, 5], x=>x > 6));
console.log(some([1, 2, 3, 4, 5], x=>x > 3));
console.log(some([1, 2, 3, 4, 5], x=>x > 0));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment