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

69
Visualizações
Return from array and never repeat

var randomArray = [
   'a','b', 'c'
];

const randomize = () => {
  let tempArray = randomArray;
  let randomIndex = Math.floor(Math.random()*randomArray.length);
  let randomItem = randomArray[randomIndex];
  
  // remove item
  randomArray.splice(randomIndex, 1);
  
  // return item
  return randomItem;
}

for (let i = 0; i < randomArray.length; i++) {
  console.log(randomize())
}

I'm trying to return items based on the length of the array (3) but for whatever reason I only return (2).

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

0

As the size of the array is reduced, you shouldn't also increment i. As both its length and i move with a step, you are ending the loop too soon. You just want to check the length:

var randomArray = [
   'a','b', 'c'
];

const randomize = () => {
  let randomIndex = Math.floor(Math.random()*randomArray.length);
  let randomItem = randomArray[randomIndex];
  
  // remove item
  randomArray.splice(randomIndex, 1);
  
  // return item
  return randomItem;
}

while (randomArray.length) {
  console.log(randomize())
}

Not your question, but note that splice returns the slice that was "spliced" out of the array, so you can use that instead of assigning the value to a variable:

var randomArray = [
   'a','b', 'c'
];

const randomize = () => {
  let randomIndex = Math.floor(Math.random()*randomArray.length);
  // remove & return the item
  return randomArray.splice(randomIndex, 1)[0];
}

while (randomArray.length) {
  console.log(randomize())
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You need a diffrerent loop and check just the length of the array.

var randomArray = ['a','b', 'c'];

const randomize = () => {
    const randomIndex = Math.floor(Math.random() * randomArray.length);
    return randomArray.splice(randomIndex, 1)[0];
}

while (randomArray.length) {
    console.log(randomize())
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You are removing the item from the list when you call randomize(), therefore your code only runs twice. Instead, you could use a copy of the array in your randomize() function. We can do this with the .slice() method. This will allow us to preserve the values of randomArray:

var randomArray = [
   'a','b', 'c'
];
let tempArray = randomArray.slice();

const randomize = () => {
  let randomIndex = Math.floor(Math.random()*tempArray.length);
  let randomItem = tempArray[randomIndex];
  
  // remove item
  tempArray.splice(randomIndex, 1);
  
  // return item
  return randomItem;
}

for (let i = 0; i < randomArray.length; i++) {
  console.log(randomize());
}

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