Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.