-
Notifications
You must be signed in to change notification settings - Fork 48
/
Guess the Number
40 lines (35 loc) · 1.2 KB
/
Guess the Number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<!DOCTYPE html>
<html>
<head>
<title>Guess the Number Game</title>
</head>
<body>
<h2>Guess the Number Game</h2>
<p>I'm thinking of a number between 1 and 100. Try to guess it!</p>
<label for="guessInput">Enter your guess:</label>
<input type="number" id="guessInput" />
<button onclick="checkGuess()">Submit Guess</button>
<h3 id="feedback"></h3>
<script>
// Generate a random number between 1 and 100
var randomNumber = Math.floor(Math.random() * 100) + 1;
var attempts = 0;
function checkGuess() {
// Get the player's guess
var userGuess = parseInt(document.getElementById("guessInput").value);
attempts++;
// Check if the guess is correct, too high, or too low
if (userGuess === randomNumber) {
document.getElementById("feedback").textContent =
"Congratulations! You guessed the number in " + attempts + " attempts.";
} else if (userGuess < randomNumber) {
document.getElementById("feedback").textContent =
"Too low! Try again.";
} else if (userGuess > randomNumber) {
document.getElementById("feedback").textContent =
"Too high! Try again.";
}
}
</script>
</body>
</html>