Created
May 26, 2016 08:34
-
-
Save dtsn/9fbc9e68a9ef31152254578389ea5e8a to your computer and use it in GitHub Desktop.
How to get the mean and standard deviation from an array in JavaScript
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 arr = [1,1,1,2,3,4,5,3,3,3,4,5,6,3,2,4,6]; | |
// first work out the mean | |
var mean = arr.reduce(function (p, c) { | |
return p + c; | |
}) / arr.length; | |
// now the stanard deviation | |
var standardDeviation = Math.sqrt(arr.map(function (l) { | |
return Math.pow(l - mean, 2); | |
}).reduce(function (p, c) { | |
return p + c; | |
}) / arr.length); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment