Created
November 27, 2013 15:11
-
-
Save niwaringo/7677271 to your computer and use it in GitHub Desktop.
undersocore.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
var calc = { | |
sum: function(array_num) { | |
return _.reduce(array_num, function(memo, num) { | |
return memo + (+num); | |
}, 0); | |
}, | |
ave: function(array_num) { | |
return this.sum(array_num) / array_num.length; | |
}, | |
median: function(array_num) { | |
var half = Math.floor(array_num.length/2); | |
array_num = _.sortBy(array_num, function(num){ | |
return num; | |
}); | |
if (array_num.length % 2) { | |
return +array_num[half]; | |
} | |
return (+array_num[half - 1] + array_num[half]) / 2; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment