Last active
August 29, 2015 14:11
-
-
Save tristaaan/84004b8c206edd1ad76e to your computer and use it in GitHub Desktop.
some array helpers.
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.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; | |
} |
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
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