-
Notifications
You must be signed in to change notification settings - Fork 296
/
Number_Guess_Game.html
53 lines (46 loc) · 1.73 KB
/
Number_Guess_Game.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
<!DOCTYPE html>
<html>
<head>
<title>Number Guessing Game</title>
</head>
<body>
<h1>Number Guessing Game</h1>
<p>Guess a number between 1 and 100:</p>
<input type="text" id="guessField">
<input type="submit" value="Submit Guess" id="guessSubmit">
<p class="message"></p>
<script>
// Generate a random number between 1 and 100
const randomNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
// Get the elements
const guessField = document.getElementById('guessField');
const guessSubmit = document.getElementById('guessSubmit');
const message = document.querySelector('.message');
// Add an event listener for the submit button
guessSubmit.addEventListener('click', checkGuess);
function checkGuess() {
const userGuess = parseInt(guessField.value);
attempts++;
if (userGuess === randomNumber) {
message.textContent = `Congratulations! You guessed the number in ${attempts} attempts.`;
message.style.color = 'green';
setGameOver();
} else if (attempts >= 10) {
message.textContent = `Game over! The correct number was ${randomNumber}.`;
message.style.color = 'red';
setGameOver();
} else {
message.textContent = userGuess < randomNumber ? 'Try a higher number.' : 'Try a lower number.';
message.style.color = 'blue';
}
guessField.value = '';
guessField.focus();
}
function setGameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
}
</script>
</body>
</html>