-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path29.html
More file actions
37 lines (34 loc) · 1.43 KB
/
Copy path29.html
File metadata and controls
37 lines (34 loc) · 1.43 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
<!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 = 0;
function checkGuess() {
attempts++;
const userGuess = parseInt(document.getElementById('guess').value);
if (userGuess === randomNumber) {
document.getElementById('result').textContent = `Congrats! You guessed the number in ${attempts} attempts.`;
document.getElementById('result').style.color = 'green';
document.getElementById('guess').disabled = true;
} else if (userGuess < randomNumber) {
document.getElementById('result').textContent = `Wrong guess! The number is higher.`;
document.getElementById('result').style.color = 'red';
} else if (userGuess > randomNumber) {
document.getElementById('result').textContent = `Wrong guess! The number is lower.`;
document.getElementById('result').style.color = 'red';
}
}
</script>
</body>
</html>