search

Home  >  Q&A  >  body text

The question is still the same, but the title is rewritten as follows: My Javascript random function fails to generate valid responses

I'm making a simple random number generator game using JS and HTML. In this game you can set minimum and maximum values ​​of random numbers.

However, when setting the variable and recording the result, an impossible number appears. For example, if I set the minimum value to 50 and the maximum value to 100, it will show answers like 58650 , 16750 , or 31450 . I noticed that the smallest number you set always appears at the end. I've tried changing the format and changing the code but nothing seems to work.

Does anyone know the solution to this problem? Your help is greatly appreciated, thank you! !

Home.html (probably not the problem):

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Random Number Generator</title>
  <link rel="stylesheet" href="style.css">
 
 
</head>
<body>
  <h1 class="Title">Random Number Generator</h1>
  <hr>
  <div class="container">
    <p> Enter 2 numbers that you want the random number to be in between</p>
    <p id="result" class="hide"></p>
    <div class="container2">
    <input id="inputmin" class="numinput" type="text">
    <p>-</p>
    <input id="inputmax" class="numinput" type="text">
   </div>
    <button id="submitbtn" class="submitbtn">Generate</button>
  </div>
  <script src="/Home.js"></script>
</body>
   </html>

Home.js:

const usermininput = document.getElementById("inputmin")
const usermaxinput = document.getElementById("inputmax")
const usersubmitBtn = document.getElementById("submitbtn")
const result = document.getElementById("result")
function random(min, max) {
  return Math.floor(Math.random() * max) + min;
}

usersubmitBtn.addEventListener("click", () =>{
  const usermin = usermininput.value

  const usermax  = usermaxinput.value
  
  console.log(random(usermin,usermax))
})

P粉682987577P粉682987577244 days ago391

reply all(1)I'll reply

  • P粉310754094

    P粉3107540942024-04-04 10:39:25

    The value attribute of the input element is a string, so you need to convert it to a number first (otherwise, performs string concatenation instead of addition). This can be done using the unary plus operator, i.e. const usermin = usermininput.value and const usermax = usermaxinput.value.

    Alternatively, use and access the valueAsNumber property.

    const usermininput = document.getElementById("inputmin")
    const usermaxinput = document.getElementById("inputmax")
    const usersubmitBtn = document.getElementById("submitbtn")
    const result = document.getElementById("result")
    function random(min, max) {
      return Math.floor(Math.random() * max) + min;
    }
    usersubmitBtn.addEventListener("click", () => {
      const usermin = usermininput.valueAsNumber;
      const usermax = usermaxinput.valueAsNumber;
      console.log(random(usermin, usermax));
    })

    Random Number Generator


    Enter 2 numbers that you want the random number to be in between

    -

    reply
    0
  • Cancelreply