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

167
Views
Javascript no redondea correctamente

Hice un juego simple de adivinanzas de números en javascript que va del 1 al 100.
Mi problema es que todo funciona, excepto cuando escribo un número decimal del 100 al 101. Quiero que mi código me muestre el mensaje "Por favor, ingrese un número del 1 al 100", pero no es así.
¿Cómo hago para que Javascript redondee esto correctamente?

 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

Simplemente coloque este código primero antes de verificar otras cosas.

 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

No redondee el número inicialmente, de lo contrario perderá la información decimal que necesita cuando desea rechazar valores entre 100 y 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!