Skip to content

Instantly share code, notes, and snippets.

@skinnyfads
Forked from FarazPatankar/robin.js
Created January 8, 2023 05:30
Show Gist options
  • Save skinnyfads/e5fd2b8f8e74a164515e13cd75d935a9 to your computer and use it in GitHub Desktop.
Save skinnyfads/e5fd2b8f8e74a164515e13cd75d935a9 to your computer and use it in GitHub Desktop.
Round robin, league matches schedule on javascript
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])
}
@skinnyfads
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment