Skip to content

Instantly share code, notes, and snippets.

@dtsn
Created May 26, 2016 08:34
Show Gist options
  • Save dtsn/9fbc9e68a9ef31152254578389ea5e8a to your computer and use it in GitHub Desktop.
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
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