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

135
Visualizações
Splicing random names from array until array is empty

I am trying to pick two random names and adding them together. Each time the name is picked, it should be removed from array. Function should run until array is empty. Output should be all names that were paired together in a string or array.

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank']
//var to pick two random names from an array
function pickTwo(arr) {
  let random = Math.floor(Math.random() * arr.length);
  let random2 = Math.floor(Math.random() * arr.length);
  while (random === random2) {
    random2 = Math.floor(Math.random() * arr.length);
  }
  //remove random and random2 from array
  //run untill there are no more names in the array
  while (arr.length > 0) {
    arr.splice(random, 1);
    arr.splice(random2, 1);
    return [arr[random], arr[random2]];
  }
}
console.log(pickTwo(names));
console.log(names);

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

First make sure you delete the 2 names you randomly selected from the array. The function you have does not do this because the arr variable that the names get deleted from is local to that function. Consider either using the global variable groovy that is initiated before the function, or returning the arr variable back from the function and then sending that back into it again. After that you simply have to make a while loop that runs the pickTwo function until there is nothing left in groovy.

var array = [];

while(groovy.length>1){
array.push(pickTwo(groovy));
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to get new random numbers each time through the while (arr.length > 0) loop. Otherwise, your random indexes may be outside the array after you splice, and you get undefined.

You need to push each pair of numbers selected during the loop onto another array, and return that array as the final result.

Also, when you do the first splice, all the indexes after random shift down. If random2 is higher than random, you'll splice the wrong element. You should splice out the higher one first.

After you splice, arr[random] and arr[random2] don't refer to the elements that were selected, since you removed them from the array. splice() returns the removed elements, you should use that result.

A simpler way to do this whole thing would be to shuffle the array. Then just get each pair of sequential elements.

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank']
//var to pick two random names from an array
function pickTwo(arr) {
  let result = [];
  while (arr.length > 0) {
    let random = Math.floor(Math.random() * arr.length);
    let random2 = Math.floor(Math.random() * arr.length);
    while (random === random2) {
      random2 = Math.floor(Math.random() * arr.length);
    }
    //remove random and random2 from array
    //run untill there are no more names in the array
    let val1, val2;
    if (random < random2) {
      val2 = arr.splice(random2, 1)[0];
      val1 = arr.splice(random, 1)[0];
    } else {
      val1 = arr.splice(random, 1)[0];
      val2 = arr.splice(random2, 1)[0];
    }
    result.push([val1, val2]);
  }
  return result;
}
console.log(pickTwo(names));
console.log(names);

about 4 years ago · Juan Pablo Isaza Relatório

0

Some issues in your code:

  • When random and random2 are such that random + 1 == random2 and random2 == arr.length - 1, then the second splice will have no effect, since the array size was already reduced.

  • As you return in the body of the while loop, there will be never a second iteration of that loop.

I would suggest to take a different approach:

  • Randomly shuffle the array
  • Return the consecutive pairs in that shuffled array

function shuffle(a) {
    var j, x, i;
    for (i = a.length - 1; i > 0; i--) {
        j = Math.floor(Math.random() * (i + 1));
        x = a[i];
        a[i] = a[j];
        a[j] = x;
    }
    return a;
}

function pickTwo(arr) {
    arr = shuffle([...arr]);
    return Array.from({length: arr.length >> 1}, (_, i) =>
        arr.slice(i*2, i*2+2)
    );
}

var names = ['Ben', 'Mike', 'Sally', 'Joe', 'Bob', 'Sam', 'Carl', 'Frank'];

console.log(pickTwo(names));

about 4 years ago · Juan Pablo Isaza Relatório
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