As a hobby I run a league of sorts for an online ice hockey game. During the season teams from Conference A play once against teams from Conference B. I want to be fair and give everyone even number of home and away matches (it matters in this game), which means I have to make every few months a huge list of matches to accomplish this.
My idea is to do all this by a click of a button with JavaScript. First question is: what kinds of techniques I should learn to make this with JavaScript/JQuery? (and optionally underscore.js, I see it has some really awesome features for shuffling for example) Remember, I'm pretty much beginner and trying to keep it as simple as possible. Below I'll write my plan, suggestions appreciated!
First of all, I have two HTML elements where the user will paste the lists of teams. After that I create two JavaScript Arrays containing the teams of each conference.
function makeArrayA() {
var arrayA = $('.confA').val().split('\n');
return arrayA;
}
Great, that gives me arrayA (and arrayB with its own, similar function) containing all the teams. Simple this far.
This next step is where I'm really stuck: Lets say I have 10 teams in both conferences, which means 5 home and 5 away games, great. But it is also possible that I have only 9 teams in conferences, or 9 in other and 10 in other. So some teams would have 5 home matches and some only 4. What to do? This makes it exponentially harder for me to catch up with the logic needed for the code.
function howManyHomeGames() {
var A = makeArrayA().length;
var B = makeArrayB().length;
}
Above function gives me a length of each of my arrays and after that I can use IF's to continue with multiple scenarios, great. Anyways THIS is where I don't know how to continue.
I know that solving big(ish) problem like this gets easier if you can cut it to smaller parts and this is what I need help with. I as a beginner am blind to see the remaining parts and would probably try to do all the rest in one huge, complicated and messy jungle of IF's and randomizing functions.
I should probably start by taking a random team from confA and confB and putting them to an array (JSON array maybe?). Then picking next one and next one and so on until the very end, by always checking if the randomly chosen pair already exists and passes some other filters too.
My Questions:
- What techniques/parts of documentation should I check out to make this project and keep it simple as possible?
- Is there something more I should pay attention to in addition to ones listed above?
- Is JSON Array a good idea for this?
- Are the code blocks I inserted above valid/good practise?
- How should I break it all to smaller parts?
- How to handle with cases of uneven number of teams? Write the whole function from scratch for each different situation?
Note: I don't want somebody doing it all for me, I just need a small push to the right direction. Thank you everyone in advance!