Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

213
Visualizações
Multiple loop generate error for different entries

I made this function to generate custom fixture, but i have an issue if the players in rounds are more than 8. I tried to add 10 playes instead of 8, and the loop return me a wrong result.

let teams = [
  { id: "Team1" },
  { id: "Team2" },
  { id: "Team3" },
  { id: "Team4" },
  { id: "Team5" },
  { id: "Team6" },
  { id: "Team7" },
  { id: "Team8" },
  {id: "Team9" },
  {id: "Team10" }               
]

// Total rounds
let totRounds = 3
// Array with rounds
let rounds = []

for (let i = 0; i < totRounds; i++) {

  // This loop add an array on enemyes teams enemies["team2,..."]
  teams.forEach(team => team.enemies = teams.filter(enemy => enemy !== team));
  // Empty Array for first round 
  const matches = [];
  // Empty Array for second round ( return )
  const matches_return = [];

  while (teams.some(team => team.enemies.length)) {

    const playing = [];
    const playing_return = []
    for (const team of teams) {
      //debugger
      if (playing.includes(team)) continue;
      const enemy = team.enemies.find(enemy => !playing.includes(enemy));
      if (!enemy) continue;

      team.enemies.splice(team.enemies.indexOf(enemy), 1);
      enemy.enemies.splice(enemy.enemies.indexOf(team), 1);

      playing.push(team, enemy);
      playing_return.push(enemy, team);
    }

    if (playing.length) matches.push(playing.map(t => t.id))
    if (playing_return.length) matches_return.push(playing_return.map(t => t.id))
  }

  // Merge 2 arrays
  const totalMatches = matches.concat(matches_return)
  rounds.push(totalMatches);

}
console.log(rounds);

This work with 8 players, but with 10 or more not. I tried to made some changes but they didn't work.

about 4 years ago · Santiago Gelvez
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda