Created
November 26, 2013 00:59
-
-
Save DanielFGray/7651721 to your computer and use it in GitHub Desktop.
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.range = function(start, count) { | |
return Array.apply(null, { length: count }) | |
.map(function(v,i){ | |
return start + i; | |
}); | |
} | |
Array.prototype.clone = function() { | |
return this.slice(0); | |
}; | |
Array.prototype.square = function() { | |
return this.map(function(n) { | |
return Math.pow(n,2); | |
}); | |
} | |
Array.prototype.cube = function() { | |
return this.map(function(n) { | |
return Math.pow(n,3); | |
}); | |
} | |
Array.prototype.average = function() { | |
return this.length > 0 ? this.sum() / this.length : NaN; | |
} | |
Array.prototype.sum = function() { | |
return this.clone().reduce(function(a, b) { | |
return a + b; | |
}); | |
} | |
Array.prototype.even = function() { | |
return this.clone().filter(function(n) { | |
return n % 2 == 0; | |
}); | |
} | |
Array.prototype.odd = function() { | |
return this.clone().filter(function(n) { | |
return n % 2 > 0; | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment