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

126
Views
JavaScript doesn't execute if statement

I am writing a simple game and can't understand why my if block doesn't work. When I click button check it executes just first if statement block, please could you help my to understand why. Here is my code.

const checkBtn = document.querySelector(".check");
const guessInput = Number(document.querySelector(".guess").value);
const againBtn = document.querySelector(".again");
const message = document.querySelector(".message");
let randomNumber = Math.floor(Math.random() * 20 + 1);
const secretNumber = document.querySelector(".secret-number");
console.log(randomNumber);

secretNumber.textContent = randomNumber;

checkBtn.addEventListener("click", function () {
  if (!guessInput) {
    message.textContent = "No Number!";
    console.log("no number");
  } else if (guessInput === randomNumber) {
    message.textContent = "Correct!";
    console.log("correct");
  } else if (guessInput > randomNumber) {
    message.textContent = "Too high!";
    console.log("too high");
  } else if (guessInput < randomNumber) {
    message.textContent = "Too low";
    console.log("too low");
  }
});

Thanks in advance!!

about 4 years ago · Santiago Trujillo
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!