Home > Back-end >  Random number upper limit and lower limit
Random number upper limit and lower limit

Time:03-31

I'm working on a random number guessing game in JavaScript. I want the user to input a lowLimit and a highLimit and have the random number generated between those two numbers. I tried hardcoding the lowLimit and highLimit as below:

let lowLimit = 5;
let highLimit = 20;
let random = Math.floor(Math.random() * highLimit);


  if (random < lowLimit) {
    random  = lowLimit;
  }
console.log(random);

and everything works well.

However, when I allow the user to input values, the random number always becomes the sum of lowLimit and upperLimit. I cannot figure this out!

My final code is this:


let lowLimit = prompt('Input the lower limit:');
let highLimit = prompt('Input the upper limit:');
let random = Math.floor(Math.random() * highLimit);
let tries = 0;

  if (random < lowLimit) {
    random  = lowLimit;
  }

  console.log(random);

let guess = prompt('Secret number generated. Enter guess:');

while (guess !== random) {
  if (guess === 'q') break;
  tries  = 1;
  if (guess > random) {
    prompt('You guessed too high. Guess again...');
  } else if (guess < random) {
    prompt('You guessed too low. Guess again...');
  } else {
    alert('You guessed correctly! You made '   tries   " guesses.");
  }
}

CodePudding user response:

This solution works. Any refactoring suggestions are welcome.

let lowLimit = Number(prompt('Input the lower limit:'));
let highLimit = Number(prompt('Input the upper limit:'));
while (!lowLimit || !highLimit) {
  lowLimit = Number(prompt('Input a valid lower limit:'));
  highLimit = Number(prompt('Input a valid upper limit:'));
}
lowLimit = Number(lowLimit);
highLimit = Number(highLimit);
let random = Math.floor(Math.random() * (highLimit - lowLimit)   lowLimit);

let guesses = 0;

  console.log(random);


  guess = prompt('Enter guess:');
while (guess !== random) {
  
  if (guess === 'q') {
    alert('Ok. Quitting... You made '   guesses   ' guesses')
      break;
  }

  guesses  = 1;
  guess = Number(guess);

  if (guess > random) {
    guess = prompt('You guessed too high. Guess again...');
  } else if (guess < random) {
    guess = prompt('You guessed too low. Guess again...');
  } else alert('You guessed correctly! You made '   guesses   " guesses.");
  
}

CodePudding user response:

A few tweaks to improve the code, and one bug fix (the case where user guesses correctly on the first try, they will receive no feedback)...

//   is concise way to coerce an int
const lowLimit =  prompt('Input the lower limit:');
const highLimit =  prompt('Input the upper limit:');
// note - we could add checks here for invalid or disordered values

// this presumes we want random to be exclusive of highLimit. if not, we'll need to tweak
const random = Math.floor(Math.random() * (highLimit - lowLimit)   lowLimit);

// we'll vary the prompt during the game
let promptMsg = 'Enter guess:', guesses = 0, guess;

// bug fix and cleanup: do while, so we always play at least once
// prompt in just one place, alter the prompt message to represent game state
do {
  guess = prompt(promptMsg);
  guesses  ;
  if (guess !== 'q') {
    guess =  guess;
    if (guess > random) {
      promptMsg = 'You guessed too high. Guess again...';
    } else if (guess < random) {
      promptMsg = 'You guessed too low. Guess again...';
    } 
  }
} while (guess !== 'q' && guess !== random);

const endMsg = guess === 'q' ? 'Ok. Quitting' : 'You guessed correctly.'
const guessesMsg = `You made ${guesses} ${guesses === 1 ? 'guess' : 'guesses'}`;
alert(`${endMsg} ${guessesMsg}`)

CodePudding user response:

We can generate a random number by using the Date.now()

let res = Date.now() % (highLimit - lowLimit 1) lowLimit.

This is because nobody can estimate the time in milisecond the code runs.

  • Related