Guess the number

About the Game: Guess the Number

Welcome to “Guess the Number” – a fun and simple game that challenges your ability to predict a random number between 1 and 10. Test your intuition and see if you can guess the correct number in just a few tries!

How to Play:

  1. You are prompted to enter a number between 1 and 10 in the input field.
  2. Click the “Submit Guess” button to submit your guess.
  3. Receive instant feedback on whether your guess is correct or not.
  4. Keep guessing until you successfully guess the correct number.

Game Rules:

  • The random number is generated between 1 and 10.
  • If your guess matches the generated number, you win!
  • If your guess is incorrect, the game will reveal the correct number.
  • Challenge yourself to guess the correct number in as few attempts as possible.
TIPS:
- Trust your instincts and go with your first guess.
- Have fun and enjoy the thrill of the game!

HTML, CSS & JAVASCRIPT SOURCE CODE.

<!DOCTYPE html>

<html lang="en">
<head>
<title>Guess the Number</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f0f0f0;
}

.container {
text-align: center;
}

h1 {
color: #333;
}

p {
margin: 10px 0;
}

input {
padding: 8px;
font-size: 16px;
margin-right: 10px;
}

button {
padding: 10px;
font-size: 16px;
background-color: #4caf50;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

#result {
margin-top: 20px;
font-size: 24px;
color: #333;
}
</style>
</head>
<body>
<div class="container">
<h1>Guess the Number</h1>
<p>Enter a number between 1 and 10:</p>
<input type="number" id="userInput" min="1" max="10" />
<button onclick="checkGuess()">Submit Guess</button>
<div id="result"></div>
</div>

<script>
function checkGuess() {
const userInput = document.getElementById("userInput").value;
const randomNumber = Math.floor(Math.random() * 10) + 1;

if (userInput == randomNumber) {
document.getElementById("result").innerHTML =
"Congratulations! You guessed the correct number!";
} else {
document.getElementById(
"result"
).innerHTML = `Wrong guess. The correct number was ${randomNumber}. Try again!`;
}
}
</script>
</body>
</html>
Design a site like this with WordPress.com
Get started