-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (36 loc) · 1.5 KB
/
script.js
File metadata and controls
46 lines (36 loc) · 1.5 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
41
42
43
44
45
46
const secretNumber = Math.floor(Math.random() * 100) + 1;
let attempts = 0;
document.getElementById('guessBtn').addEventListener('click', () => {
const userGuess = Number(document.getElementById('guessInput').value);
const feedback = document.getElementById('feedback');
const attemptsDisplay = document.getElementById('attempts');
if (!userGuess || userGuess < 1 || userGuess > 100) {
feedback.textContent = "Please enter a valid number between 1 and 100.";
return;
}
attempts++;
attemptsDisplay.textContent = `Attempts: ${attempts}`;
if (userGuess === secretNumber) {
feedback.textContent = `${userGuess} is correct! 🎉 You guessed it!`;
feedback.style.color = "#2ecc71";
} else if (userGuess < secretNumber) {
feedback.textContent = `${userGuess} Too low! Try a higher number.`;
feedback.style.color = "#3498db";
} else {
feedback.textContent = `${userGuess} Too high! Try a lower number.`;
feedback.style.color = "#e67e22";
}
});
document.getElementById('play-again').onclick = function() {
// Reset the input field
document.getElementById('guess-input').value = '';
// Reset attempts
attempts = 0;
document.getElementById('attempts-display').textContent = attempts;
// Hide the Play Again button
this.style.display = 'none';
// Reset any messages
document.getElementById('result-message').textContent = 'No previous guess yet';
// Optionally, reinitialize the game (if there's any game logic)
initializeGame();
};