Working with arrays, choosing only one die

1

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

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 do not have to 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

Logic is simply not fitting ...

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 caught within the second for, I do not know what I could do ..

    
asked by anonymous 01.04.2015 / 19:25

1 answer

1

To draw, since you do not want to repeat the value, with each draw remove the item drawn (like 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 sort all items loop to continue drawing while there are items to be drawn, something like this:

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

It is basically this, in Array sorteados , you will have the items drawn in the order to manipulate as you wish, in your case grouping them by 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 this 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;
}
    
01.04.2015 / 20:00