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

265
Vistas
When i make use of the getRandomNumber(); it gets a random number on page reload but does not change the random number on button click

OnClick of my submit button, the random function is supposed to refresh but mine remains the same. So i tried a different approach and it worked. To get the desired result i wrote my code inside the a playGame function Like this.

window.onload = function() {
    document.getElementById("number-submit").addEventListener("click", playGame);
    document.getElementById("restart-game").addEventListener("click", initGame);

}

function playGame() {
    let numberGuess = document.getElementById("number-guess").value;
    console.log(numberGuess);

    var randomNumber = Math.floor((Math.random() * 3) + 1);
    console.log(randomNumber);

    if (numberGuess > randomNumber) {
        console.log("Too High, Try Again");
    } else if (numberGuess < randomNumber) {
        console.log("Too Low, Try Again");
    } else {
        console.log("Congratulations This Is Correct!!");
    }
}

i tried writhing a getRandomNumner() function after trying out my solution to see if i could make my code better but using the code provided below does not provide the desired result for me. When i make use of the getRandomNumber(); it gets a random number on page reload but does not change the random number on click of the button. What are my doing wrong?? This is my getRandomNumber function.

let correctNumber = getRandomNumber();
console.log(correctNumber);

function getRandomNumber(){

let randomNumber = Math.floor((Math.random() * 100) + 1);

    return randomNumber;
}

WHAT ARE MY DOING WRONG WITH THE getRandomNumber() ??

about 4 years ago · Santiago Trujillo
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