Created
September 9, 2018 04:35
-
-
Save afuggini/ccc74896676751b50f471ec571ea9f5d to your computer and use it in GitHub Desktop.
Weighted mean (average) in Javascript ES6
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
const sumArrayValues = (values) => { | |
return values.reduce((p, c) => p + c, 0) | |
} | |
const weightedMean = (factorsArray, weightsArray) => { | |
return sumArrayValues(factorsArray.map((factor, index) => factor * weightsArray[index])) / sumArrayValues(weightsArray) | |
} | |
weightedMean([251, 360, 210], [0.1, 0.5, 0.7]); | |
// => 270.8461538461539 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment