Created
October 6, 2023 09:07
-
-
Save akiross/bc088154ddcbe14eb45f5bea93495289 to your computer and use it in GitHub Desktop.
Custom function for google sheet to compute the Borda count
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Computes the borda count of strings in a range. | |
* @param {Array<Array<string>>} input The range with strings. | |
* @return A string with the winning string and its score. | |
* @customfunction | |
*/ | |
function BORDA_WINNER(input) { | |
let votes = {}; | |
let n_rows = input.length; | |
let n_cols = input[0].length; | |
for (let row = 0; row < input.length; row++) { | |
for (let col = 0; col < input[row].length; col++) { | |
const cell = input[row][col]; | |
if (cell === "") | |
continue; | |
if (cell in votes) | |
votes[cell] += n_rows - row; | |
else | |
votes[cell] = n_rows - row; | |
} | |
} | |
let winner = "No Winner"; | |
let count = -1; | |
for (const [key, value] of Object.entries(votes)) { | |
if (value > count) { | |
count = value; | |
winner = key; | |
} | |
} | |
return `${winner} ${count}`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment