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

145
Views
Generating a random number from user Input

I've been trying to make a code that prompts the user to enter a minimum value and a maximum value in order to return a random value from the interval of those two. It's been hours now, and I can't really understand what I'm doing wrong. Also forgive me, I'm a student, so I'm still trying to understand these basic stuff.

I've tried running code, but it returns "NaN". I think the problem here is about the variables or something, so can someone try and point it out?

function userInput(low,high){
         return prompt("Enter a minimum value:");
         return prompt("Enter a maximum value:");
    }
    function generateNum(){
    return Math.floor(Math.random()*max-min+1)+min;
    }
    function outputNum(q){
        document.getElementById("text1").innerHTML = q;
    }
    outputNum("Randomly generated number: "+generateNum());
    
    var min = parseInt(userInput());
    var max = parseInt(userInput());
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Here is a working snippet. I corrected a few points (missing parentheses, a second return in a function that can never be reached and several more).

function generateNum(){
  [min,max]=[+prompt("Enter a minimum value:"),
             +prompt("Enter a maximum value:")].sort();
  return min+Math.floor(Math.random()*(max-min+1));
}
document.getElementById("text1").innerHTML = "Randomly generated number: "+generateNum();
<div id="text1"></div>

I simplified it further and added a .sort() to it. Als long as there are actual numbers entered into the prompt dialogues there will be min and max values. No need for an optional error message.

about 4 years ago · Juan Pablo Isaza Report

0

Another approach

<!DOCTYPE html>
<html>
    <title> Random Between Min & Max </title>
<body>
<h2>Random Between Min & Max</h2>

<button onclick="randBetween()">Try it</button>

<p id="random"></p>

<script>
function randBetween() {
  let min = parseInt(prompt("Please enter minimum number"));
  let max = parseInt(prompt("Please enter maximum number"));
  if (min !=null && max != null && min < max) {
  let randBetween = Math.floor(Math.random() * (max - min + 1)) + min;
  document.getElementById("random").innerHTML = randBetween;
  }else{
  alert("Enter min and max value and max value must be greater than min");
  }
}
</script>
</body>
</html>
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!