-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path28.html
More file actions
40 lines (37 loc) · 1.58 KB
/
Copy path28.html
File metadata and controls
40 lines (37 loc) · 1.58 KB
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 lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Number Guessing Game</title>
</head>
<body>
<h1>Number Guessing Game</h1>
<p>Guess the number between 1 and 100</p>
<input type="number" id="guess" min="1" max="100">
<button onclick="checkGuess()">Submit Guess</button>
<p id="result"></p>
<script>
const randomNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 5;
function checkGuess() {
const userGuess = parseInt(document.getElementById('guess').value);
if (userGuess === randomNumber) {
document.getElementById('result').textContent = `Congratulations! You guessed the number in ${6 - attempts} attempts.`;
document.getElementById('result').style.color = 'green';
document.getElementById('guess').disabled = true;
} else {
attempts--;
if (attempts > 0) {
document.getElementById('result').textContent = `Wrong guess! ${attempts} attempts remaining.`;
document.getElementById('result').style.color = 'red';
} else {
document.getElementById('result').textContent = `Sorry, you've used all your attempts. The number was ${randomNumber}.`;
document.getElementById('result').style.color = 'red';
document.getElementById('guess').disabled = true;
}
}
}
</script>
</body>
</html>