Round-Robin is a way to rotate an array of items such that each item can be paired with every other item exactly once. In sports, this is a way to ensure that each team plays every other team exactly once.
Imagine you have an array of 10 teams, and each team has a number:
[1,2,3,4,5,6,7,8,9,10]
Your goal is to write a function that takes an input array and returns an array of arrays of arrays.
For example, for an array input of [1,2,3,4]
the result would be:
[
[ // ---- round 1 -----
[1, 4], // 1 plays 4
[3, 2], // 3 plays 2
],
[ // ---- round 2 -----
[1, 3], // 1 plays 3
[2, 4], // 2 plays 4
],
[ // ---- round 3 -----
[1, 2], // 1 plays 2
[4, 3], // 4 plays 3
]
]
- Split the array in half
- Reverse the second array
The result should look like this:
Take the first element of the first array, and match it with the first element of the second array, and so on. Visually, that looks like:
In code, that looks like:
[
[1, 10],
[2, 9 ],
[3, 8 ],
[4, 7 ],
[5, 6 ],
]
- Keep the first element of the first array in its position (it will never change)
- Rotate the other numbers counter-clockwise
Which ends up looking like:
Take the first element of the first array, and match it with the first element of the second array, and so on. Visually, that looks like:
[
[1, 9],
[10, 8],
[2, 7],
[3, 6],
[4, 5],
]
Your round robin function should never return duplicate matches.
You will be working in Jasmine to solve this problem.
Open index.html
in a browser to see the tests run. As you make changes to your round robin js file, hit refresh (CMD+R) in your browser to run the tests again.
- Install LoDash in index.html
- Use LoDash Methods, write code to pass specs
In a separate file called ui.html
create an HTML page that:
- Loads up jQuery
- Can you load it from the CDN?
- From Bower?
- Has a textarea where users can enter a list of teams, separated by newlines
- Has a button that users can click to generate a schedule
- When the button is clicked, print the schedule according to the wireframe
- When the button is clicked again, clear the previous schedule, and recreate
NOTE: use custom CSS for this - no frameworks.
- Create a copy of
index.html
- Remove the reference to LoDash
- Create a new function
roundRobinPure
and solve it with pure JS
'nuff said.