Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

164
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda