Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

178
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda