Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

266
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!