0

My kid's baseball team has 13 kids. Each kid is supposed to rotate through the positions so that they all get equal time at each position. We have 4 outfielders, by the way, as these kids are only 7 years old.

I tried to write a little Javascript algorithm to rotate them through fairly. I came up with the following, but it doesn't seem to be as fair as I thought it would be, and it's confusing. I am sure there's an easier way. Suggestions?

var people = new Array("Amelie","Avery","Brennan","Clayton","Devin","Flynn","Haydn","Jack","Kai","Liam","Max","Maxi","Sterling");
    var people_copy = people.slice(0);
    var jobs = new Array("Pitcher","Catcher","Third Base","Short Stop","Second Base","First Base","Right Field","Ctr Right","Ctr Left","Left");
    var jobs_copy = jobs.slice(0);
    var result_set = new Array();

    for(i=0;i<jobs_copy.length;i++){
        j = i%jobs.length;
        result_set[j] = new Object();
          for(h=0;h<people_copy.length;h++){
                  if((jobs[0]) && (people[0])){
                    jobby = jobs[0].trim();
                    persony = people[0].trim();
                    result_set[j][jobby] = persony; 
                  }
                  var shifted = people.shift();
                  people.push(shifted);

                  var jobs_shifted = jobs.shift();
                  jobs.push(jobs_shifted);    
            }
            var shifted = people.shift();
            people.push(shifted);
          }

    for(i=0;i<result_set.length;i++){

        console.log("\nIteration: " + (i+1));

        for(h=0;h<jobs_copy.length;h++){
            l = jobs_copy[h];
            console.log(l + '=' + result_set[i][l]);

        }
    }
Mreider
  • 83
  • 1
  • 7
  • Why not just create two lists (players, jobs), then rotate one of the lists...? i.e. `jobs = [a, b, c]`, `players = [1, 2, 3]`, then next week, `jobs = [b, c, a]`, `players = [1, 2, 3]` – mash Mar 14 '14 at 04:53
  • Because you might have fewer jobs than players or vice versa. – Mreider Mar 14 '14 at 17:04

2 Answers2

0
// Init the list.
var people = ["Amelie","Avery","Brennan","Clayton","Devin","Flynn","Haydn","Jack","Kai","Liam","Max","Maxi","Sterling"];
var jobs = ["Pitcher","Catcher","Third Base","Short Stop","Second Base","First Base","Right Field","Ctr Right","Ctr Left","Left"];

for (var c=people.length; c>0; c--) {
    var firstToLast = people[0]; // Backup first value
    people.shift (); // Remove from the list 
    people.push(firstToLast); // Append the name to the end
    console.log (people); // ["Avery", "Brennan", "Clayton", "Devin", "Flynn", "Haydn", "Jack", "Kai", "Liam", "Max", "Maxi", "Sterling", "Amelie"]

    // To print a list of jobs
    for (var i=0; i<jobs.length; i++) {
        console.log (people[i] + ': ' + jobs[i]);
    }
    console.log ('-----');
}

If all you want is to rotate the people list, this should do the job.

Edit: Loop added.

Jixun
  • 140
  • 10
0

Since there are more players than jobs, I've added Not playing jobs to your array so that we can simply rotate the jobs and have everything line up nicely.

Below is a simple solution that rotates the jobs array by a specified number:

var people = ["Amelie", "Avery", "Brennan", "Clayton", "Devin", "Flynn", "Haydn", "Jack", "Kai", "Liam", "Max", "Maxi", "Sterling"];
var jobs = ["Pitcher", "Catcher", "Third Base", "Not playing", "Short Stop", "Second Base", "First Base", "Not playing", "Right Field", "Ctr Right", "Ctr Left", "Left", "Not playing"];


function getPositions(gameNumber) {
    for (var i = 0; i < people.length; i++) {
        console.log("Player: " + people[i] + ", " + jobs[(i + gameNumber) % jobs.length]);
    }
}

If you wish to play 10 games, then invoke the function 10 times with the gameNumber argument ranging from 1-10.

You can also shuffle the initial players array, just to make sure everything is fair. For more information: How can I shuffle an array?

Community
  • 1
  • 1
Peter
  • 3,998
  • 24
  • 33
  • If we get a new player, or shrink the jobs, the algorithm should "just work" without having to add things like "not playing." – Mreider Mar 14 '14 at 18:11
  • I like Jixun's solution up above :) – Mreider Mar 14 '14 at 18:16
  • 1
    @Mreider, I hope you know that the solution you selected will leave the same person benched up to three games in a row though. :) – Peter Mar 14 '14 at 18:43