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

205
Vistas
javascript random numbers generator

I'm trying to generate 500 random numbers between 1 and 50 and then divide them to my 5 ranges and then print the percentage of each range in the form of stars. I have written the below piece of code but it only generates 1 number instead of 500. can anyone help me fix this problem? I am new to javascript and any help or advice would be appreciated. Thank you.

    let temp = Math.floor((Math.random() * 50) + 1);
    let bin1 = 0;
    let bin2 = 0;
    let bin3 = 0;
    let bin4 = 0;
    let bin5 = 0;
    
    if(temp >= 1 && temp < 11){
    bin1++;
    document.write(bin1 + " numbers are randomly generated between 01 - 10.<br>");
    }
        else if(temp >= 11 && temp < 21){
    bin2++;
    document.write(bin2 + " numbers are randomly generated between 11 - 20.<br>");
    }
        else if(temp >= 21 && temp < 31){
    bin3++;
    document.write(bin3 + " numbers are randomly generated between 21 - 30.<br>");
    }
        else if(temp >= 31 && temp < 41){
     bin4++;
    document.write(bin4 + " numbers are randomly generated between 31 - 40.<br>");
    }
        else if(temp >= 41 && temp < 51){
     bin5++;
     document.write(bin5 + " numbers are randomly generated between 41 - 50.<br>");
    }
    
    document.write("Histogram of stars as a percentage of the number of values are displayed below: <br>");
    
    for(let x = 0; x < bin1*100/500; x++){
    document.write("01 - 10: ");
    document.write("*");
    }
    for(let x = 0; x < bin2*100/500; x++){
    document.write("11 - 20: ");
    document.write("*");
    }
    for(let x = 0; x < bin3*100/500; x++){
    document.write("21 - 30: ");
    document.write("*");
    }
    for(let x = 0; x < bin4*100/500; x++){
    document.write("31 - 40: ");
    document.write("*");
    }
    for(let x = 0; x < bin5*100/500; x++){
    document.write("41 - 50: ");
    document.write("*");
    }
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Looks like you have to wrap in a for-loop.

Trying to do what you are looking for, see the example below:

function generateRandomNumber() {
  return Math.floor(Math.random() * 50);
}

const ranges = {
  "1-11": [],
  "11-21": [],  // instead of array we can have value 0, and increment that incase dont want to track the values in future
  "21-31": [],
  "31-41": [],
  "41-51": [],
}

function updateRanges(num) {
  if(num >= 41 && num < 51) ranges["41-51"].push(num);
  else if(num >= 31 && num < 41) ranges["31-41"].push(num);
  else if(num >= 21 && num < 31) ranges["21-31"].push(num);
  else if(num >= 11 && num < 21) ranges["11-21"].push(num);
  else ranges["1-11"].push(num);
}

for (let i = 0; i < 500; i++) {
  const random =  generateRandomNumber();
  updateRanges(random);
}

const output =  document.getElementById("output");
Object.keys(ranges).forEach(key => {
  output.innerHTML = output.innerHTML + 
  
  `<p>numbers are randomly generated between ${key} : ${ranges[key].length}</p>` 
})
<div id="output"></div>

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