skip to Main Content

The code just keep saying to insert larger number or smaller number. but don’t get it to the actual number and break, though the logic is fine in my opinion. Sometimes the console doesn’t print anything and it keeps roaming like infinite loop. Can anyone help me to understand what’s going on!??

//  Modify the guessing game you created to allow the user to guess within a dynamic range (e.g., "Guess a number between 50 and 150"). Ensure the user cannot enter a guess outside of this range.
while (true) {
  let randNumber = Math.floor(Math.random() * 101) + 50;
  let userGuess = prompt("Guess the Number: (50-150)");
  userGuess = parseInt(userGuess);
  if (!isNaN(userGuess) && userGuess >= 50 && userGuess <= 150) {
    if (userGuess == randNumber) {
      console.log("congrats! right guess");
      break;
    } else if (userGuess < randNumber) {
      console.log("Try large Number");
    } else if (userGuess > randNumber) {
      console.log("Try small number");
    }
  } else {
    console.log("Wrong input. Try a valid Number between (50-150)");
  }
}

2

Answers


  1. On every iteration of the loop, you generate a new random number, which is probably not what you meant.

    Put the random number generation before the loop to give the end user multiple opportunities at guessing it:

    let randNumber = Math.floor(Math.random()*101)+50;
    while (true) {
        // etc...
    
    Login or Signup to reply.
  2. The main issue in your code is because you’re generating a new random number in every iteration of the loop. To fix this, move the line of code that generates the number outside of your while loop.

    That being said, running this logic in a while loop is not a good approach. A better idea would be to define a function which contains the logic to request the user input and handle their choice and call this recursively until they get the right number.

    Also note that you’ll need to add a slight delay before showing the next prompt() so that the UI/console has time to update.

    Here’s a working example with the above changes made:

    const getUserGuess = (target) => {
      setTimeout(() => {
        let userGuess = prompt("Guess the Number: (50-150)");
        userGuess = parseInt(userGuess, 10);
        
        if (isNaN(userGuess) || userGuess < 50 || userGuess > 150) {
          console.log("Invalid input. Try a valid Number between (50-150)");
          getUserGuess(target);
          return;
        }
    
        if (userGuess == target) {
          console.log("Congrats! Correct guess");
        } else if (userGuess < target) {
          console.log("Try a larger number");
          getUserGuess(target);
        } else if (userGuess > target) {
          console.log("Try a smaller number");
          getUserGuess(target);
        }
      }, 50);
    }
    
    getUserGuess(Math.floor(Math.random() * 101) + 50);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search