Netbyzz

#A052
Guess the number game

HTML :

<!-- CODE = #A052 -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>NETBYZZ A052</title>
    <link rel="stylesheet" href="style.css">
  </head>
  <body>
    <div class="container">
      <h1>Guess the Number (1-100)</h1>
      <p>Guess the number within 10 tries:</p>
      <input type="text" id="guessField" />
      <button onclick="checkGuess()">Submit Guess</button>
      <p id="message"></p>
    </div>

    <script src="script.js"></script>
  </body>
</html>

CSS :

body {
  font-family: "Arial", sans-serif;
  background-color: #f0f0f0;
  margin: 0;
  padding: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}
.container {
  background-color: #fff;
  padding: 20px;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
  text-align: center;
}
h1 {
  margin-top: 0;
  color: #333;
}
p {
  margin-bottom: 20px;
  color: #555;
}
input[type="text"] {
  padding: 5px 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 5px;
  margin-right: 10px;
}
button {
  padding: 5px 15px;
  font-size: 16px;
  background-color: #007bff;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
}
button:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}
#message {
  margin-top: 20px;
  color: #333;
  font-weight: bold;
}

JS :

const randomNumber = Math.floor(Math.random() * 100) + 1;
let guesses = 0;

function checkGuess() {
  let userGuess = parseInt(document.getElementById("guessField").value);
  let message = document.getElementById("message");

  if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
    message.textContent =
      "Please enter a valid number between 1 and 100.";
    return;
  }

  guesses++;

  if (userGuess === randomNumber) {
    message.textContent = `Congratulations! You guessed the number ${randomNumber} in ${guesses} tries.`;
    disableInput();
  } else if (guesses === 10) {
    message.textContent = `Game over! The number was ${randomNumber}.`;
    disableInput();
  } else if (userGuess < randomNumber) {
    message.textContent = "Try a higher number.";
  } else if (userGuess > randomNumber) {
    message.textContent = "Try a lower number.";
  }
}

function disableInput() {
  document.getElementById("guessField").disabled = true;
  document.querySelector("button").disabled = true;
}

Source Code