Skip to content

Instantly share code, notes, and snippets.

@akiross
Created October 6, 2023 09:07
Show Gist options
  • Save akiross/bc088154ddcbe14eb45f5bea93495289 to your computer and use it in GitHub Desktop.
Save akiross/bc088154ddcbe14eb45f5bea93495289 to your computer and use it in GitHub Desktop.
Custom function for google sheet to compute the Borda count
/**
* 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