1

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!

5
  • JSON is short for "JavaScript Object Notation" - basically it is a string. What you probably mean is "JavaScirpt Object" but everything in JS is an object - so just JavaScript Array then... Dec 10, 2017 at 13:39
  • Probably what you really want here would be an object that holds the arrays and the values such as the count of home and away games and who plays whom in a schedule object in that object. Dec 10, 2017 at 13:41
  • Yeah, "object that holds the arrays" sounds like what I need. I'm still "a bit" clunky with all the terms. Thank you for your input!
    – Roopeg
    Dec 10, 2017 at 15:02
  • Look for other questions for the schedule part, no need to re-invent the wheel here. i.e. a quick search yields codereview.stackexchange.com/questions/159817/… Dec 10, 2017 at 15:28
  • Note that the way an uneven number of teams is often paired is to create a "bi" week where they do not play anyone or something similar. Dec 10, 2017 at 15:34

1 Answer 1

0

Just to get you started, I create an object (myObject) that can hold some stuff. This is probably slightly more complicated than "basic" but you can use that object to "hold" stuff like your league teams, functions etc.

I put in some console.log you can un-comment those to see them run. Per your question this is just a "get you started" with stuff.

You will want some other function like the team schedule creation etc.

// create object to hold stuff
var myObject = {
  mydata: {
    league: {
      teams: []
    },
    schedules: {}
  },
// function objects
  myfuncs: {
    init: function() {
      //console.log("inits:", this); // shows that 'this' is myfuncs
      // get the teams when click button
      $('#getteams').on('click', this.getTeams);// no clear, keeps adding...
      this.newfunc();// show use of added function
    },
    getTeam: function(myteams, name, whereat) {
      var tconf = $(whereat).val();
      // console.log("w:", whereat, "conf=:", tconf);
      var t = tconf.split('\n');// caution, blank lines become a team here...might be better to separate by comman "myteam,mysecondteam"
      console.log("t,tconf:", t, tconf);
      $.each(t, function(index, value) {
        // add to my team array (passed in here as myteams)
        myteams.push({
          teamName: value,
          teamConference: name
        });
      });
    },
    getTeams: function() {
      // console.log(this); // 'this' is the button here passed by jQuery
      // verbose called named function, there are other ways to do this
      myObject.myfuncs.getTeam(myObject.mydata.league.teams, "A", '#conf-a');
      myObject.myfuncs.getTeam(myObject.mydata.league.teams, "B", '#conf-b');
      // you will need to get each "list of teams" based on the conf name, this does not do that
      alert(myObject.mydata.league.teams.length);// however many got added based on paste
    },
  }
};
// add a new function to my object:
myObject.myfuncs.newfunc = function(){alert("innewfunc");};
myObject.myfuncs.init(); // call to init my things
alert(myObject.mydata.league.teams.length); // alerts 0, not processed yet
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<textarea id="conf-a"></textarea>
<textarea id="conf-b"></textarea>
<button id="getteams">Get Teams</button>

2
  • Note that this is just a "get you started" and does NOT answer all the questions - it would be too broad of a question, those really are separate questions that should be posed based on something you have "tried first" and need help with, like how to make the schedule from uneven counts etc. Dec 10, 2017 at 15:20
  • Huge thanks! It took some time to understand all of it (your comments saved me), but you really helped me a lot! I think this will keep me busy for a few days.
    – Roopeg
    Dec 10, 2017 at 22:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.