Error in the division by groups ramdomicamente

1

Good morning. I am making a code in javascript, to split an array of names into groups. It is just an example, but I will use it in a football system that I participate in.

I want to do everything dynamically, but let's break it down: I have this code, which divides by teams (I used the name of medications because I already had a list here), and I wanted it to be divided by a defined number (4 teams of 6 participants). But it is generating an object with 2 arrays of 5 names, and 3 arrays of 4 names.

Could you help me make the code generate 3 teams of 6 participants, and the last team of 4 participants?

var array = ['TRAXONOL 100MG',
  'CETOMICOSS 200MG',
  'CETONEO 200MG',
  'UROVIT 100MG',
  'UROVIT 100MG',
  'UNI VIR 200MG',
  'AFTILIV',
  'LEIBA SACHE',
  'LEIBA CAPS',
  'SUPOSITORIO GLICERINA',
  'GLICERIN',
  'SEAKALM LIQUIDO',
  'SEAKALM COMP',
  'VERTIGIUM 10MG',
  'FLUCANIL 150MG',
  'TENSALIV 10MG',
  'FONT D GTS',
  'VITACIN 1G',
  'CENEVIT 1G',
  'TONICO VITAL',
  'NUTRI HOMEM',
  'NUTRI MAIS LIQUIDO'
];

function distributePlayers(names, numberOfTeams) {
  var ret = {};
  var teamCounter = 0;

  for (var i = 0; i < names.length; ++i) {
    if (!ret["array" + teamCounter]) {
      ret["array" + teamCounter] = [];
    }
    ret["array" + teamCounter].push(names[i]);
    if (++teamCounter == numberOfTeams) {
      teamCounter = 0;
    }
  }
  return ret;
}

var arrays = distributePlayers(array, 4);
console.log(arrays)

Example here: link

Thank you!

    
asked by anonymous 26.09.2018 / 14:24

2 answers

1

This works:

for (var i = 0; i < names.length; ++i) {
    if (!ret["array" + teamCounter]) {
        ret["array" + teamCounter] = [];
    }
    ret["array" + teamCounter].push(names[i]);
    if (ret["array" + teamCounter].length == Math.ceil(names.length / numberOfTeams)) {
        teamCounter++;
    }
}

The code fills teams one by one until the desired size is reached (or no more teams).

link

    
26.09.2018 / 14:42
1

The way you are doing this is going around all the players and distributing between the teams.

I think it would be more performative to go through just the array of teams and distribute players, so the loop would be much smaller.

See example below

let itens = ['TRAXONOL 100MG',
  'CETOMICOSS 200MG',
  'CETONEO 200MG',
  'UROVIT 100MG',
  'UROVIT 100MG',
  'UNI VIR 200MG',
  'AFTILIV',
  'LEIBA SACHE',
  'LEIBA CAPS',
  'SUPOSITORIO GLICERINA',
  'GLICERIN',
  'SEAKALM LIQUIDO',
  'SEAKALM COMP',
  'VERTIGIUM 10MG',
  'FLUCANIL 150MG',
  'TENSALIV 10MG',
  'FONT D GTS',
  'VITACIN 1G',
  'CENEVIT 1G',
  'TONICO VITAL',
  'NUTRI HOMEM',
  'NUTRI MAIS LIQUIDO'
];


function distributePlayers(names, numberOfTeams) {
  let itens = names.slice(0)

  // Quantidade de jogadores por time
  const numberOfPlayers = Math.ceil(itens.length / numberOfTeams)

  // Cria o array de times e distribui os jogadores
  const final = Array.from(Array(numberOfTeams).keys())
            .map(p => itens.splice(0, numberOfPlayers))

  return final
}

var arrays = distributePlayers(itens, 6)

console.log(arrays)
    
26.09.2018 / 15:06