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

163
Views
Javascript not rounding properly

I made a simple number guessing game in javascript which goes from 1 to 100.
My problem is that everything works, except when I type a decimal number from 100 to 101. I want my code to give me the "Please enter a number from 1 to 100" message then, but it doesn't.
How do I get Javascript to round this properly?

let randomNumber = Math.floor(Math.random() * 100); // Random number

function guess() {
  let inp = document.getElementById("number").value; // Reading input
  let inpRounded = Math.floor(inp);

  // Random number is higher
  if (inpRounded < randomNumber && inpRounded >= 1 && inpRounded <= 100) {
    document.getElementById("result").innerHTML = "Higher";
  }
  // Random number is lower
  else if (inpRounded > randomNumber && inpRounded >= 1 && inpRounded <= 100) {
    document.getElementById("result").innerHTML = "Lower";
    // Input matches random number 
  } else if (inpRounded == randomNumber && inpRounded >= 1 && inpRounded <= 100) {
    document.getElementById("result").innerHTML = "You won!";
  }
  // Input is weird
  else if (inpRounded == null) {
    document.getElementById("result").innerHTML = "Please enter a valid number";
  }
  // Input is lower than 1 or higher than 100
  else if (inpRounded < 1 || inpRounded > 100) {
    document.getElementById("result").innerHTML = "Please enter a number from 1 to 100";
  }
  // Other issue
  else {
    document.getElementById("result").innerHTML = "Error"
  }
}
<h2>Enter a number from 1 to 100</h2>
<input type="number" placeholder="Type your guess here" id="number">
<input type="button" value="Go!" onclick="guess()">
<p id="result"></p>

about 4 years ago · Santiago Gelvez
2 answers
Answer question

0

Just put this code first before you check other things.

  if (inpRounded == null) {
    document.getElementById("result").innerHTML = "Please enter a valid number";
    return;
  }
  if (inpRounded < 1 || inpRounded > 100) {
    document.getElementById("result").innerHTML = "Please enter a number from 1 to 100";
    return;
  }
about 4 years ago · Santiago Gelvez Report

0

Don't round the number initially, otherwise you'll lose the decimal information you need when you want to reject values between 100 and 101.

function guess() {
  const value = document.getElementById("number").value; // Reading input
  const numValue = Number(value);
  if (numValue < 1 || numValue > 100) {
    document.getElementById("result").textContent = "Please enter a number from 1 to 100";
    return;
  }
  const inpRounded = Math.floor(numValue);
  // etc
about 4 years ago · Santiago Gelvez 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!