Skip to content

Instantly share code, notes, and snippets.

@FarazPatankar
Forked from mikeyakymenko/robin.js
Created April 16, 2019 10:19
Show Gist options
  • Save FarazPatankar/ed4af0da42546a83246e41fa42f44c0d to your computer and use it in GitHub Desktop.
Save FarazPatankar/ed4af0da42546a83246e41fa42f44c0d 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])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment