Error in the division by groups

Asked

Viewed 59 times

1

Good morning. I’m making code in javascript, to divide an array of names into groups. It’s just an example, but I’ll use it in a system for football.

I want to do everything dynamically, but let’s go in parts: I have this code, which divides by teams (I used the name of medicines because I already had a list here), and I wanted it to be divided by defined number (4 teams of 6 participants). Only it is generating an Object with 2 arrays of 5 names, and 3 arrays of 4 names.

They could 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: http://jsfiddle.net/guilhermelirio85/0djLyg9p/

Thank you!

2 answers

2


It 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++;
    }
}

Code fills teams one by one until desired size is reached (or no more teams).

http://jsfiddle.net/0djLyg9p/3/

  • Perfect! Thank you and I will study the code better!

  • 1

    That one == instead of >= ai in the comparison of the end - will not give problem when the division of names.length / numberOfTeams not whole?

  • 1

    Javascript has no entire division. Math.ceil() always returns an integer.

1

The way you’re doing this is going through all the players and distributing between the teams.

I think it would be more performative if you just go through the array of teams and distribute the 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)

  • Perfect the answer too, thank you very much!!

  • A question: because if I put the creation of the array within a function, it only runs once?

  • 1

    Because in the example I’m using splice to add items to the group. splice remove the elements of the original item. What you can do is inside the function before you start is clonar the player array for a variable and will remove the items from it keeping the original array intact. let cloneItens = Object.assign({}, itens)

  • 1

    I changed the answer to function to suit the example

  • I could leave it that way, @Danilo. I’ll run some tests!!

  • Alias, now that I realize I don’t shuffle array names. But I’ll see how to do that. Thanks again!

  • 1

    Ideally you leave the responsibility of the method only to distribute the players, already passing to it the shuffled players.

  • 1

    You can use this function to shuffle https://gist.github.com/Atlas7/c04bbef301dfce601f36324886635558

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.