-
Notifications
You must be signed in to change notification settings - Fork 1
/
borda.js
61 lines (51 loc) · 1.42 KB
/
borda.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
function borda(rows) {
let votes = rows[0];
let tally = {};
let text_to_des = {};
for (i = 0; i < votes.length; i++) {
let option = `${String(votes[i].option_text)} (${String(votes[i].option_description)})`;
text_to_des[String(votes[i].option_text)] = votes[i].option_description;
if (tally.hasOwnProperty(option)) {
tally[option] += votes[i].rank;
}
else {
tally[option] = 0;
tally[option] += votes[i].rank;
}
}
let number_of_options = Object.keys(tally).length;
for (let vote in tally) {
tally[vote] = (number_of_options + 1) * (votes.length / number_of_options) - tally[vote];
}
let arr = Object.keys(tally).map(function(key) {
return [key, tally[key]];
});
let sorted = arr.sort(function(a, b) {
return b[1] - a[1];
});
for (i = 0; i < sorted.length; i++) {
for (let element in text_to_des) {
if (`${String(element)} (${String(text_to_des[element])})` === sorted[i][0]) {
sorted[i].push(String(element));
sorted[i].push(text_to_des[element]);
}
}
}
//push a value of true into the arrays denoting the top ranked choice(s)
if (sorted.length !== 0) {
let max = sorted[0][1];
sorted[0].push(true);
for (i = 1; i < sorted.length; i++) {
if (max > sorted[i][1]) {
break;
}
else {
sorted[i].push(true);
}
}
}
return sorted;
}
module.exports = {
borda: borda
};