Guess the Number Game

Why? Learn random numbers, loops, and conditions.

<h2>Guess the Number (1-100)</h2>
<input type="number" id="guess">
<button onclick="checkGuess()">Check</button>
<p id="message"></p>

<script>
let secret = Math.floor(Math.random()*100) + 1;

function checkGuess() {
  let guess = parseInt(document.getElementById("guess").value);
  
  if(guess === secret) {
document.getElementById("message").innerText = "πŸŽ‰ Correct! You guessed it!";
} else if(guess < secret) {
document.getElementById("message").innerText = "Too low! Try again.";
} else {
document.getElementById("message").innerText = "Too high! Try again.";
} } </script>

Explanation:

  • Generates a random number between 1–100.
  • User enters guess β†’ checks if it’s too low, too high, or correct.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *