/
average.js
39 lines (37 loc) · 1.4 KB
/
average.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Averaging with equal weights
// Takes in an array of ints
function avgEqualWeight(arrayOfPercentages){
var numberOfPercentages = arrayOfPercentages.length;
var sumOfPercentages = 0;
for (var i = 0; i < numberOfPercentages; i++) {
sumOfPercentages += arrayOfPercentages[i];
}
var average = sumOfPercentages / numberOfPercentages;
// Round to one decimal place
round(average, 1)
return average;
}
// Averaging with unequal weights
// Takes in a dict with key/value pairs of ints
function avgUnequalWeight(dictionaryOfPercentagesAndWeights){
var weightedAverageDecimalVal = 0;
for (var percentage in dictionaryOfPercentagesAndWeights){
// Get weight by accessing the percentage key's value
var weight = dictionaryOfPercentagesAndWeights[percentage];
// Convert the percentages and their weights into decimals
percentageDecimalVal = percentage / 100;
weightDecimalVal = weight / 100;
// Calculate the weighted percentage (in decimal val)
weightedPercentDecimalVal = percentageDecimalVal * weightDecimalVal;
// Add weighted percentage to weighted average
weightedAverageDecimalVal += weightedPercentDecimalVal;
}
var weightedAverage = weightedAverageDecimalVal * 100;
// Round to one decimal place
round(weightedAverage, 1)
return weightedAverage;
}
private function round(value, precision) {
var multiplier = Math.pow(10, precision || 0);
return Math.round(value * multiplier) / multiplier;
}