Last active
July 25, 2016 03:40
-
-
Save khaled0fares/a519caf2b18531ecdbd51808f0a41ab3 to your computer and use it in GitHub Desktop.
implementation of "every" function in JS
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
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