Files
30-seconds-of-code/test/standardDeviation/standardDeviation.js
2018-01-17 13:40:40 -05:00

8 lines
321 B
JavaScript

const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};
module.exports = standardDeviation