Skip to content

Instantly share code, notes, and snippets.

@afuggini
Created September 9, 2018 04:35
Show Gist options
  • Save afuggini/ccc74896676751b50f471ec571ea9f5d to your computer and use it in GitHub Desktop.
Save afuggini/ccc74896676751b50f471ec571ea9f5d to your computer and use it in GitHub Desktop.
Weighted mean (average) in Javascript ES6
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