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

104
Visualizações
JavaScript - RNG min, average, max need some help in connecting results

This is a assignment to be run on jsfiddle so it has a split html and javascript code. html is given. The task is to make it work with the given html. I did as much as I could with my limited knowledge in javascript. I pretty much understand what I am missing in this code, I just don't know how to write it. I have nothing connecting the results with the randomizer. Can someone tell me how I would do it? I checked out other threads regarding min-average-max, but I don't think the way they are doing it would work for mine.

##html code

`<h2>Problem #4: Data Simulation</h2>
<button onclick="runSimulation()">Run Simulation</button><br>
Largest number: <input type="text" id="max" disabled><br>
Smallest number: <input type="text" id="min" disabled><br>
Average: <input type="text" id="average" disabled><br>`

##javascript code

const LOWER_BOUND = 10000;
const UPPER_BOUND = 60000;

function runSimulation() {

    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;


document.querySelector("#max").value = max;
document.querySelector("#min").value = min;
document.querySelector("#average").value = average;

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

0

Since you are returning from the function, your DOM manipulation never works. Here is a similar working example:

const LOWER_BOUND = 10000;
const UPPER_BOUND = 60000;

function runSimulation(){
   const [num1, num2] = [
   Math.floor(Math.random() * (UPPER_BOUND - LOWER_BOUND) + LOWER_BOUND),
   Math.floor(Math.random() * (UPPER_BOUND - LOWER_BOUND) + LOWER_BOUND)
   ]
   
   const max = Math.max(num1, num2), min = Math.min(num1, num2)
   const average = (max + min) / 2
   document.querySelector("#max").value = max;
   document.querySelector("#min").value = min;
   document.querySelector("#average").value = average;
}
<body>
<h2>Problem #4: Data Simulation</h2>
<button onclick="runSimulation()">Run Simulation</button><br>
Largest number: <input type="text" id="max" disabled><br>
Smallest number: <input type="text" id="min" disabled><br>
Average: <input type="text" id="average" disabled><br>
</body>

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