Weighted Average
by soulwire
JavaScript
const ratings = [
{ value: 1, weight: 0.2 },
{ value: -0.5, weight: 0.9 },
{ value: 0.8, weight: 0.1 },
{ value: 0.6, weight: 0.8 }
]
const weights = ratings.map(rating => rating.weight)
const totalWeight = weights.reduce((a, b) => a + b)
const normalisedWeights = weights.map(w => w / totalWeight)
let totalScore = 0
ratings.forEach((rating, index) => {
totalScore += rating.value * normalisedWeights[index]
})
console.table({ weights, normalisedWeights })
console.log('total score:', totalScore)