JSFiddle - React, Tailwind, and code Playground
by leggetter
JavaScript
let names = ["Aristoula","Eric","Esther", "Georgios", "Luis","Marco","Parth","Paul","Phil", "Sandy"];
shuffleArray(names)
// Schedule single round `j` for 'n' teams:
function round(n, j) {
let m = n - 1;
let round = Array.from({length: n}, (_, i) => (m + j - i) % m); // circular shift
round[round[m] = j * (n >> 1) % m] = m; // swapping self-match
return round;
}
// Schedule matches of 'n' teams:
function fixture(n) {
let rounds = Array.from({length: n - 1}, (_, j) => round(n, j));
return Array.from({length: n}, (_, i) => ({
id: names[i],
matches: rounds.map(round => names[round[i]])
}));
}
/* Randomize array in-place using Durstenfeld shuffle algorithm */
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// Example:
console.log(fixture(names.length));