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

182
Visualizações
Math.random in a while loop

Can someone help me? I would like to make a simple code where dom generates a random number from 0-14 and adds it to the ran array and it will only stop once all numbers from 0-14 are inside the array.

function random () {
let ran = []
let dom = Math.floor(Math.random() * 15)
while (a != a.length(15)) {
   a.push(b)
   return a
 }
}

console.log(random)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Logic.

  • random function keeps on pushing random mnumbers from 0-14 to the ran array
  • checkAllNumberpresent creates a number array from 0 - 14 using Array.from(Array(15).keys())
  • This function accept the input array and check whther any numbers from the input array is missing in the numberArray
  • If all are present, the function returns false and this stops the while loop execution.

Working Fiddle

function checkAllNumberpresent(arr) {
  const numberArray = Array.from(Array(15).keys());
  const missingNodes = numberArray.filter(item => arr.indexOf(item) === -1);
  return missingNodes.length == 0;
}
function random() {
  const ran = [];
  while (!checkAllNumberpresent(ran)) {
    let dom = Math.floor(Math.random() * 15);
    ran.push(dom)
  }
  return ran
}

console.log(random())

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