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

207
Visualizações
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 Respostas
Responde à pergunta

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 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