From de8a5b3fed30c4d7f6b389c93b0bccf50c65e481 Mon Sep 17 00:00:00 2001 From: zuzanna-maria Date: Mon, 6 Feb 2023 12:34:05 +0100 Subject: [PATCH] refactoring condorcet function --- www/form/inner.js | 75 +++++++++++++++++++++++++++++------------------ 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/www/form/inner.js b/www/form/inner.js index ff96d147b..bfa2ff8c1 100644 --- a/www/form/inner.js +++ b/www/form/inner.js @@ -2429,13 +2429,25 @@ define([ var rendered = renderTally(count, empty, showBars); return h('div.cp-charts.cp-bar-table', rendered); }, - // Creates every possible combination pair of given options - getCombinations: function(array, size) { + + + showCondorcetWinner: function(answers, opts) { + + var _answers = parseAnswers(answers); + + optionArray = [] + opts.values.forEach(function (option) { + optionArray.push(option.v) + }) + + // Creates every possible combination pair of given options + var getCombinations = function(array, size) { + function p(t, i) { - if (t.length === size) { - result.push(t); - return; - } + if (t.length === size) { + result.push(t); + return; + } if (i + 1 > array.length) { return; } @@ -2446,23 +2458,14 @@ define([ var result = []; p([], 0); return result; - }, - - showCondorcetWinner: function(answers, opts) { - - var _answers = parseAnswers(answers); - - optionArray = [] - console.log(opts) - opts.values.forEach(function (option) { - optionArray.push(option.v) - }) + } + var comparePairs = function(_answers) { listOfLists = [] Object.keys(_answers).forEach(function(a) { listOfLists.push(_answers[a].msg[Object.keys(_answers[a].msg)[0]]) }) - var pairs = this.getCombinations(optionArray, 2) + var pairs = getCombinations(optionArray, 2) var scoringDict = {} @@ -2490,24 +2493,34 @@ define([ scoringDict[pair] = pair[1]; } }) + return scoringDict - //Counts the number of pairwise comparisons won by each option - listOfMatches = [] - optionArray.forEach(function(option) { - var winningMatches = {} - winningMatches[option] = 0 - Object.keys(scoringDict).forEach(function(key) { - if (key.includes(option) && scoringDict[key] == option) { - winningMatches[option] ++; } - }) - listOfMatches.push(winningMatches); + + var wonMatches = function(_answers) { + var scoringDict = comparePairs(_answers) + //Counts the number of pairwise comparisons won by each option + listOfMatches = [] + optionArray.forEach(function(option) { + var winningMatches = {} + winningMatches[option] = 0 + Object.keys(scoringDict).forEach(function(key) { + if (key.includes(option) && scoringDict[key] == option) { + winningMatches[option] ++; + } + }) + listOfMatches.push(winningMatches); }) - console.log(listOfMatches) + return listOfMatches + + } + + var calculateWinner = function(_answers) { //Checks for tie let scores = [] + var listOfMatches = wonMatches(_answers) listOfMatches.forEach(function(match) { scores.push(Object.values(match)) scores = scores.flat() @@ -2535,6 +2548,10 @@ define([ return condorcetWinner + } + + return(calculateWinner(_answers)) + }, icon: h('i.cptools.cptools-form-list-ordered')