0

So I'm working on a function that splits up the contents of an array into different "teams":

    generateTeams(players, numTeams)
    {
        var tempArray = [];
        tempArray = players.slice();

        var arrayLength = tempArray.length;
        var playerPerTeam = 
        Math.floor(tempArray.length/numTeams);
        console.log("chunk size is:", playerPerTeam)
        var results = [];

        while (tempArray.length){
           console.log("length",tempArray.length)
           results.push(tempArray.splice(0, playerPerTeam));
        } 
    }

If I feed it this input:

players = ["Juan", "Jeff", "Derek", "Bob", "Elizabeth", "Alex", "Isabelle"]
numTeams = 3

the function returns this:

["Juan", "Jeff"] ["Derek", "Bob"]["Elizabeth", "Alex"] ["Isabelle"]

So it returns 4 teams instead of 3. I was expecting one team to have 3 players and the other 2 teams to have 2 players instead of it making a separate team.

There's probably a simple solution I'm missing but I've been looking at how to split up this array into a certain number of teams and I can't quite figure it out.

Any help would be appreciated!

oturan
  • 3
  • 2
  • There are some answers on this thread that may be helpful for you https://stackoverflow.com/a/37826698/10789668 – Luis Sardon May 13 '20 at 04:31
  • @LuisSardon I think if I was asking for a "players per team" input instead of "number of teams" input this would solve my issue. I don't know how to translate what I see there to my problem though. – oturan May 13 '20 at 05:11

3 Answers3

0

Use Math.ceil().

In this case, arrayLength = 7, numTeams = 3. Means, 7/3 = 2.3333

Math.floor(2.3333) will result 2 whereas, Math.ceil(2.3333) will result 3.

Vaibhav
  • 1,412
  • 1
  • 10
  • 14
  • I tried changing it to Math.ceil but I get undefined behavior if I do that. I tested it with this: var players = ["Juan", "Jeff", "Derek", "Bob", "Elizabeth", "Alex", "Isabelle"]; console.log(generateTeams(players,3)) – oturan May 13 '20 at 05:06
0

function generateTeams(players, numTeams)
    {
        var tempArray = [];
        tempArray = players.slice();

        var arrayLength = tempArray.length;
        var playerPerTeam = 
        Math.floor(tempArray.length/numTeams);
        console.log("chunk size is:", playerPerTeam)
        var results = [];
        while (results.length < numTeams ){
           results.push(tempArray.splice(0, playerPerTeam));
        }
        if(tempArray.length){
        results[results.length-1]=[...results[results.length-1],...tempArray]
        }
        return results;
    }

var players = ["Juan", "Jeff", "Derek", "Bob", "Elizabeth", "Alex", "Isabelle"];
var players2 = ["Juan", "Jeff", "Derek"];
var players3 =  ["Juan", "Jeff", "Derek", "Bob", "Elizabeth"]
console.log(generateTeams(players,3));
console.log(generateTeams(players2,3));
console.log(generateTeams(players3,3))

Make use of es6 array spread

if you want playerPerTeam to have 3 uses Math.ceil instead

nivendha
  • 792
  • 7
  • 16
  • I ran the code you provided with this input to test: var players = ["Juan", "Jeff", "Derek"]; console.log(generateTeams(players,3)) I expected it to give me this: ["Juan"], ["Jeff"], ["Derek"] but instead it gave me this: ["Juan"], ["Jeff", "Derek"] – oturan May 13 '20 at 05:02
  • So I think this is working a bit better, but I tested it with var players = ["Juan", "Jeff", "Derek", "Bob", "Elizabeth"]; and it creates 5 teams for some reason instead of 3 teams. – oturan May 13 '20 at 05:15
  • if u want maximum players to be populated in each team use `Math.ceil` instead of `Math.floor` – nivendha May 13 '20 at 05:28
  • Ahh okay. Is there a way to spread out the players better? For players3, there's 3 players in team 3 but only one player in both team 1 and 2. Is there a way to make it so it doesn't end up with one team having way more players? – oturan May 13 '20 at 05:29
  • use `Math.ceil` for that – nivendha May 13 '20 at 05:30
0

This is another solution for this issue using array reduce:

function generateTeams(players, numTeams) {   
    var playerPerTeam = 
        Math.floor(players.length/numTeams);
        var team = 0;
        return players.reduce(function(results, player) {
            if(
               results[team] 
               && results[team].length === playerPerTeam 
               && team < numTeams - 1
            ) {
             team++;
            }
            
           if (!results[team]) {
             results[team] = [];
            }
            
           results[team].push(player);
            
          
         return results;
        }, []);
    }
    
var players = ["Juan", "Jeff", "Derek", "Bob", "Elizabeth", "Alex", "Isabelle"];
var players2 = ["Juan", "Jeff", "Derek"];
var players3 =  ["Juan", "Jeff", "Derek", "Bob", "Elizabeth"];
console.log(generateTeams(players,3));
console.log(generateTeams(players2,3));
console.log(generateTeams(players3,3));
Fermin Perdomo
  • 411
  • 4
  • 11