Skip to content

Instantly share code, notes, and snippets.

@khaled0fares
Last active July 25, 2016 03:40
Show Gist options
  • Save khaled0fares/a519caf2b18531ecdbd51808f0a41ab3 to your computer and use it in GitHub Desktop.
Save khaled0fares/a519caf2b18531ecdbd51808f0a41ab3 to your computer and use it in GitHub Desktop.
implementation of "every" function in JS
Array.prototype.Every = function(callback){
let i = 0, len = this.length;
for(i; i < len; i++){
if(!callback(this[i])){
return false
}
}
return true
}
Array.prototype.Some = function(callback){
let i = 0, len = this.length;
for(i; i < len; i++){
if(callback(this[i])){
return true
}
}
return false
}
let odd = function(ele){return ele % 2 === 1 }
console.log([1,3,5].Every(odd)); // true
console.log([1,4,5].Every(odd)); // false
console.log([1,3,5].Some(odd)); // true
console.log([1,4,5].Some(odd)); // true
console.log([2,4,6].Some(odd)); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment