Using JavaScript/jQuery to generate fixtures
-
12-09-2019 - |
Question
I'm putting together a tool for a colleague which helps to create a nice fixture list. I got about 2/3 through the tool, collecting various data ... and then I hit a brick wall. It's less of a JavaScript problem and more of a maths/processing brainblock.
Lets say I have 4 teams, and they all need to play each other at home and away. Using this tool - http://www.fixturelist.com/ - I can see that a home and away fixture with 4 teams would take 6 weeks/rounds/whatever. For the life of me, though, I can't work out how that was programmatically worked out.
Can someone explain the logic to process this?
For info, I would use this existing tool, but there are other factors/features that I need to work in, hence doing a custom job. If only I could understand how to represent that logic!
Solution
In your example of 4 teams, call them a, b, c and d:
- a has to play b, c, d
- b has to play c, d (game against a already included in a's games)
- c has to play d (game against a already included in a's games, against b already included in b's games)
If they need to play at home and away, that's 12 games. You can play at most 4/2 = 2 games a week, so that's 6 weeks.
With n
teams you need x
games, where:
x = ((n-1 + n-2 + n-3 ...) * 2)
This takes y
weeks, where:
y = x/(n/2) = 2x/n
This can be simplified with an arithmetic series fairly easily, or calculated with a for loop if you want.