Skip to content

Instantly share code, notes, and snippets.

@skinnyfads
Created January 24, 2023 16:47
Show Gist options
  • Save skinnyfads/b12a1f3d4d578a2374324fd6a5750a45 to your computer and use it in GitHub Desktop.
Save skinnyfads/b12a1f3d4d578a2374324fd6a5750a45 to your computer and use it in GitHub Desktop.
league matches schedule on javascript [ typescript version ]
interface ISchedule {
homeTeam: ITeam;
awayTeam: ITeam;
}
interface ITeam {
name: string;
strength: number;
points: number[];
numericId: number;
tier: number;
}
type ITeamLeague = ITeam | undefined;
function generateLeague(teams: ITeamLeague[]) {
const schedules: ISchedule[][] = [];
const participants = teams.slice();
if (participants.length % 2) {
participants.push(undefined);
}
const totalRounds = participants.length;
for (let currentWeek = 0; currentWeek < (totalRounds - 1) * 2; currentWeek++) {
schedules[currentWeek] = [];
for (let i = 0; i < totalRounds / 2; i++) {
const teamA = participants[i];
const teamB = participants[totalRounds - 1 - i];
if (teamA && teamB) {
if (currentWeek % 2 === 1) {
schedules[currentWeek].push({ homeTeam: teamA, awayTeam: teamB });
} else {
schedules[currentWeek].push({ homeTeam: teamB, awayTeam: teamA });
}
}
}
participants.splice(1, 0, participants.pop());
}
return schedules;
}
export default generateLeague;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment