Last active
May 12, 2019 19:36
-
-
Save meetbryce/87f4314a4622428f5bf3 to your computer and use it in GitHub Desktop.
Average or Sum the values in an array using Javascript (and Underscore.js)
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
// requires Underscore.js | |
// uses jQuery style funciton declaration (if you aren't using jQuery, simply re-arrange the declaration) | |
function sum(arr) { | |
// returns the sum total of all values in the array | |
return _.reduce(arr, function(memo, num) { return memo + num}, 0); | |
} | |
function average(arr) { | |
// returns the average of all values in the array | |
return sum(arr) / arr.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is cool