[SOLVED] I want the user to guess a random number but every time I refresh the page a new random number generates

Issue

I need to create a random number that the user has to guess. When I refresh the page it appears the generate a new random number.

var userPrompt = prompt("Guess a number");
var ran = Math.floor(Math.random() * 10);
    
function ranNumGame(ran) {
    
      if (userPrompt == ran) {
        return "Congrats";
      }
      else if (ran < userPrompt) {
        return "Lower";
      }
      else if (ran > userPrompt) {
        return "Higher";
      }
}
    
console.log(ranNumGame(ran));

Solution

var randomNumber = Math.floor(Math.random() * 10);
var userInput = prompt("Pick a number between 0 and 10");

while (userInput != randomNumber) {
  alert("nope");

  if (userInput < randomNumber) {
    userInput = prompt("guess higher");
  } else if (userInput > randomNumber) {
    userInput = prompt("guess lower");
  } else if (userInput == randomNumber) {
    alert("congrats");
    console.log("congrats");
  }
}

Answered By – goatstash

Answer Checked By – Marilyn (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published.