displayResults.js
2.24 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/**
* Created by Techniv on 07/12/2016.
*/
var RelativePlacement = require('../../lib/relative-placement');
var data = require('./results_data.json');
var util = require('util');
console.log('############################');
console.log('# Details of result matrix #');
console.log('############################');
console.log('');
console.log('The cadidate are display in order of placement');
console.log('');
console.log('Column:');
console.log('Expected | (*error)Candidate\t[Votes]\t[Sum matrix (Sum score)]');
console.log('');
console.log('');
console.log('');
console.log('');
data.forEach((testCase, num) => {
console.log("#"+ num + ": "+testCase.comment);
var election = new RelativePlacement();
var votes = compileVotes(testCase);
election.addCandidates(votes[0]);
election.addVotes(votes);
var result = election.getResult();
var expected = testCase.result;
var candidates = election.candidateList;
result.forEach((candidateName, place) => {
var log = [];
var candidate = candidates[candidateName];
log.push(expected[place]);
log.push('\t|\t');
(candidateName != expected[place]) ? log.push('*') : log.push(' ');
log.push(candidateName);
log.push('\t');
log.push(util.format(candidate.votes));
log.push('\t[');
for (var i = 0; i < candidate.placements.length; i++){
log.push(i==0 ? ' ' : ', ');
log.push(candidate.placements[i]);
log.push('(');
log.push(candidate.cumulativePlacement[i]);
log.push(') ');
}
log.push(' ]');
console.log(log.join(''));
});
console.log('-------------------------------------------------------------------');
console.log('');
console.log('');
});
/**
* @param {TestData} testData
* @return {String[][]}
*/
function compileVotes(testData){
var votes = testData.votes;
var compileVotes = [];
var candidates = Object.keys(votes);
var voteNumber = votes[candidates[0]].length;
for(let i = 0; i < voteNumber; i++){
let compileVote = [];
for(let name in votes){
compileVote[votes[name][i]-1] = name;
}
compileVotes[i] = compileVote;
}
return compileVotes;
}