Skip to content

Instantly share code, notes, and snippets.

@DanielFGray
Created November 26, 2013 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielFGray/7651721 to your computer and use it in GitHub Desktop.
Save DanielFGray/7651721 to your computer and use it in GitHub Desktop.
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