Created
February 5, 2017 21:11
-
-
Save cmstead/cb4328c627fedb3ccad9467e68af4416 to your computer and use it in GitHub Desktop.
Simple stats API example
This file contains hidden or 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 statsApi = (function () { | |
'use strict'; | |
var square = pow(2); | |
var squareRoot = pow(0.5); | |
function getStandardDeviation (values) { | |
var xBar = getMean(values); | |
var computedValues = values.map(subtractMeanAndSquare(xBar)); | |
return squareRoot(getMean(computedValues)); | |
} | |
function getMean (values) { | |
return sum(values) / values.length; | |
} | |
function subtractMeanAndSquare (mean) { | |
return function (value) { | |
return square(value - mean); | |
}; | |
} | |
function pow(power) { | |
return function (value) { | |
return Math.pow(value, power); | |
} | |
} | |
function sum (values) { | |
return values.reduce(add, 0); | |
} | |
function add (a, b) { | |
return a + b; | |
} | |
return { | |
getMean: getMean, | |
getStandardDeviation: getStandardDeviation | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment