-
Notifications
You must be signed in to change notification settings - Fork 66
/
guess-the-num.js
35 lines (30 loc) · 1.21 KB
/
guess-the-num.js
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
// Generate a random number between 1 and 100
const randomNumber = Math.floor(Math.random() * 100) + 1;
const guessSubmit = document.getElementById('guessSubmit');
const guessField = document.getElementById('guessField');
const message = document.getElementById('message');
let attempts = 0;
guessSubmit.addEventListener('click', checkGuess);
function checkGuess() {
const userGuess = Number(guessField.value);
attempts++;
if (userGuess === randomNumber) {
message.textContent = `Congratulations! You guessed the number ${randomNumber} in ${attempts} attempts.`;
message.style.color = 'green';
setGameOver();
} else if (attempts >= 5) {
message.textContent = `Game Over! The correct number was ${randomNumber}.`;
message.style.color = 'red';
setGameOver();
} else if (userGuess < randomNumber) {
message.textContent = `Try a higher number. Attempts: ${attempts}`;
message.style.color = 'red';
} else if (userGuess > randomNumber) {
message.textContent = `Try a lower number. Attempts: ${attempts}`;
message.style.color = 'red';
}
}
function setGameOver() {
guessField.disabled = true;
guessSubmit.disabled = true;
}