Created
June 24, 2018 12:40
-
-
Save mikeyakymenko/af85e234577fc4cc145cf9d20b11f1b0 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