Skip to content

Instantly share code, notes, and snippets.

@tristaaan
Last active August 29, 2015 14:11
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 tristaaan/84004b8c206edd1ad76e to your computer and use it in GitHub Desktop.
Save tristaaan/84004b8c206edd1ad76e to your computer and use it in GitHub Desktop.
some array helpers.
Array.prototype.max = function(){
return this.reduce(function(prev, current){
if (current > prev){
prev = current;
}
return prev;
}, 0);
}
Array.prototype.min = function(){
return this.reduce(function(prev, current){
if (current < prev){
prev = current;
}
return prev;
}, 0);
}
Array.prototype.sum = function(){
return this.reduce(function(prev, current){
return prev + current;
}, 0);
}
Array.prototype.average = function(){
return this.sum() / this.length;
}
function max(arr){
return arr.reduce(function(prev, current){
if (current > prev){
prev = current;
}
return prev;
}, 0);
}
function min(arr){
return arr.reduce(function(prev, current){
if (current < prev){
prev = current;
}
return prev;
}, 0);
}
function sum(arr){
return arr.reduce(function(prev, current){
return prev + current;
}, 0);
}
function average(arr){
return sum(arr) / arr.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment