Working with arrays, choosing only one data

Asked

Viewed 165 times

1

I can have a composition of N elements in an array that can be arranged as follows for example:

array with A, B, C and D... 12 possibilities.

arr1 = ["A|B","A|C","A|D","B|A","B|C","B|D","C|A","C|B","C|D","D|A","D|B","D|C"];

array with A, B and C... 9 possibilities.

arr2 = ["A","B","C","A|B","A|C","B|A","B|C","C|A","C|B"];

I need to create a draw so that I don’t repeat one of the elements, Exp:

arrDeSaida = ["B|A","D|C"]; //Válido
arrDeSaida = ["B|A","A|C"]; //inválido

arrDeSaida = ["B|C","A"]; //Válido
arrDeSaida = ["B|A","B"]; //inválido

The logic just isn’t adding up...

qtdElementos = ["A","B","C","D"];

for (var i = 0; i < qtdElementos.length; i++) {
  for (var x = 0; x < qtdElementos.length; x++) {
    if(x != i){
      arr1.push(qtdElementos[i]+'|'+qtdElementos[x]);
    }
  }
}

for (var i = 0; i <= qtdElementos.length/2; i++) {
  if(arr1.length >= 2){
    posicao = arr1[i].split("|");
    for (var j = ; j < arr1.length; j++) {

    }
  }     
}

I stalled inside the second go, I don’t know what I could do..

  • If in the arr1 already has for example A|C the "A" and "C" can no longer be drawn?

  • yes, that’s the idea

1 answer

1

To draw, since you do not want to repeat the amount, at each draw remove the drawn item ( equal a bingo for example, where the drawn ball is removed). You can do something like this:

var sorteados = new Array();

function sortear(){
    // sorteia uma posição do array
    var index = Math.floor(Math.random() * paraSortear.length);
    // adiciona o item sorteado em um array de sorteados
    sorteados.push(paraSortear[index]);
    // remove o item sorteado do sorteio
    paraSortear.splice(index, 1);
}

Already to draw all items make a loop to continue drawing while there are items to be drawn, something like this:

while(paraSortear.length > 0){
    sortear();
}

That’s basically it, in the Array sorteados, you will have the items drawn in order to handle as you wish, in your case grouping 2 in 2.

To group as you quoted from 2 to 2 you can do so:

function agrupar(array){
    var arrayAgrupado = new Array();
    for (var i = 0; i < array.length; i = i+2) {
        arrayAgrupado.push(array[i] + (array[i + 1] ? "|" + array[i + 1] : ""));
    }
    return arrayAgrupado;
}

With that call:

var agrupados = agrupar(sorteados);

Complete online example

Full example:

var paraSortear = ["A", "B", "C", "D", "F"];

var sorteados = new Array();

function sortear() {
  // sortea uma possição do array
  var index = Math.floor(Math.random() * paraSortear.length);
  // adiciona o item sorteado em um array de sorteados
  sorteados.push(paraSortear[index]);
  // remove o item sorteado do sorteio
  paraSortear.splice(index, 1);
}

function agrupar(array) {
  var arrayAgrupado = new Array();
  for (var i = 0; i < array.length; i = i + 2) {
    arrayAgrupado.push(array[i] + (array[i + 1] ? "|" + array[i + 1] : ""));
  }
  return arrayAgrupado;
}

while (paraSortear.length > 0) {
  sortear();
}

var agrupados = agrupar(sorteados);

// limpa body
document.body.innerHTML = "";

function printJSON(value) {
  // para imprimir no DOM
  document.body.appendChild(document.createTextNode(JSON.stringify(value, null, 4)));
}

printJSON(agrupados);
body {
  white-space: pre;
  font-family: monospace;
}

  • So in case I have a list of clashes that have already been drawn: jogosFeitos= ["A|B","D|C"]; and a list of clashes suggested by my system, Exp: jogosSugeridos= ["A|C","A|D","B|C","B|D", "C|A","C|B","D|A","D|B"]; That was the point of my question, I think I really should not have expressed myself well rs

  • I don’t get it @Marcelobonifazio?

  • From the suggested games and the already made games I will draw the next game

  • @Marcelobonifazio, I could not understand it uses doubt, nor its scenery. If you can edit your question with this comment information and maybe some clear and explanatory example of the scenario and problem you need to solve, later I can try to propose another solution to your problem.

Browser other questions tagged

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