-
-
Save skinnyfads/e5fd2b8f8e74a164515e13cd75d935a9 to your computer and use it in GitHub Desktop.
Round robin, league matches schedule on javascript
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
let teams = [ | |
'Tigers', | |
'Foofels', | |
'Drampamdom', | |
'Lakebaka' | |
] | |
const roundRobin = (teams) => { | |
let schedule = [] | |
let league = teams.slice() | |
if (league.length % 2) { | |
league.push('None') | |
} | |
let rounds = league.length | |
for (let j=0; j<(rounds-1)*2; j ++) { | |
schedule[j] = [] | |
for (let i=0; i<rounds/2; i++) { | |
if (league[i] !== 'None' && league[rounds-1-i] !== 'None') { | |
if (j % 2 == 1) { | |
schedule[j].push([league[i], league[rounds-1-i]]) | |
} else { | |
schedule[j].push([league[rounds-1-i], league[i]]) | |
} | |
} | |
} | |
league.splice(1, 0, league.pop()) | |
} | |
return schedule | |
} | |
let leagueSchedule = roundRobin(teams) | |
for (let p=0; p<leagueSchedule.length; p++) { | |
console.log(leagueSchedule[p]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
TypeScript version: https://gist.github.com/skinnyfads/b12a1f3d4d578a2374324fd6a5750a45