Generate match of matches per round
-
I am able to generate matches and display on the screen. But it happens that you can't have the same game in the same round.
I created a function
verificarRodada()
to avoid this, but she seems not avoiding. Anybody help?function gerarRodadas(arrTimes, arrPartidas) { qtd_total_partidas = arrPartidas.length; qtd_rodadas = (arrTimes.length-1)*2; qtd_partidas_por_rodada = arrTimes.length/2;
arr_pos = new Array(qtd_total_partidas); iniciarArray(arr_pos, qtd_total_partidas); for(i=0; i<qtd_rodadas; i++) { cond=0; while(cond < qtd_partidas_por_rodada) { rand = Math.floor(Math.random() * qtd_total_partidas); if(arr_pos[rand] == 0) { if(verificarRodada(i, this.arrRodadas, arrPartidas[rand])) { mandante = arrPartidas[rand].mandante; visitante = arrPartidas[rand].visitante; estado = arrPartidas[rand].estado; this.arrRodadas.push(new Array(i, mandante, visitante, estado)); arr_pos[rand] = 1; cond++; } } } } this.arrRodadas.forEach(rodada => { part = "Rodada : " + (rodada[0]+1) + " - " + rodada[1] + " vs " + rodada[2] + " - " + rodada[3]; criarElemento("add_partidas", "p", part); })
}
function verificarRodada(rodada, rodadas, partida)
{
mandante = partida.mandante;
visitante = partida.visitante;rodadas.forEach(item => { if(item[0] == rodada) { if(item[1] == mandante || item[1] == visitante || item[2] == mandante || item[2]== visitante) { return false; } } }) return true;
}
-
I realized a possible problem in the function check Round
Instead of using forEach, use find
function verificarRodada(rodada, rodadas, partida) { mandante = partida.mandante; visitante = partida.visitante;
rodadas.find(item => { return (item[0] == rodada) && (item[1] == mandante || item[1] == visitante || item[2] == mandante || item[2]== visitante) });
}
ForEach is not stopped with return false. Use find or every.