-
Notifications
You must be signed in to change notification settings - Fork 0
/
bored.html
64 lines (59 loc) · 2.05 KB
/
bored.html
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Guessing Game</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
input, button {
padding: 10px;
margin: 10px;
font-size: 16px;
}
#message {
margin-top: 20px;
font-size: 18px;
}
</style>
</head>
<body>
<h1>Number Guessing Game</h1>
<p>Guess the number between 1 and 100!</p>
<input type="number" id="guessInput" min="1" max="100" placeholder="Enter your guess" required>
<button onclick="checkGuess()">Submit Guess</button>
<button onclick="resetGame()">Reset Game</button>
<div id="message"></div>
<script>
// Initialize the game
let numberToGuess = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
function checkGuess() {
const userGuess = parseInt(document.getElementById("guessInput").value);
const messageDiv = document.getElementById("message");
attempts++;
if (isNaN(userGuess) || userGuess < 1 || userGuess > 100) {
messageDiv.innerText = "Please enter a valid number between 1 and 100!";
return;
}
if (userGuess < numberToGuess) {
messageDiv.innerText = `Too low! Attempts: ${attempts}`;
} else if (userGuess > numberToGuess) {
messageDiv.innerText = `Too high! Attempts: ${attempts}`;
} else {
messageDiv.innerText = `Congratulations! You guessed it in ${attempts} attempts.`;
}
}
function resetGame() {
numberToGuess = Math.floor(Math.random() * 100) + 1;
attempts = 0;
document.getElementById("guessInput").value = "";
document.getElementById("message").innerText = "";
}
</script>
</body>
</html>