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

279
Views
Random number upper limit and lower limit

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.");
  }
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

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.");
  
}
about 4 years ago · Juan Pablo Isaza Report

0

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}`)

about 4 years ago · Juan Pablo Isaza Report

0

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.

about 4 years ago · Juan Pablo Isaza Report
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!