-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
37 lines (33 loc) · 1.19 KB
/
app.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
36
37
// script.js
document.addEventListener('DOMContentLoaded', () => {
const form = document.getElementById('guessForm');
const input = document.getElementById('guessInput');
const message = document.getElementById('message');
const result = document.getElementById('result');
const maxTries = 10;
let randomNumber = Math.floor((Math.random()*10)+1);
console.log(randomNumber);
let tries = 0;
form.addEventListener('submit', (e) => {
e.preventDefault();
const userGuess = parseInt(input.value, 10);
tries++;
if (userGuess === randomNumber) {
result.textContent = 'Congratulations You guessed the number....';
result.style.color = 'white';
resetGame();
} else if (userGuess > randomNumber) {
message.textContent = 'Too high! Try again.';
} else {
message.textContent = 'Too low! Try again.';
}
input.value = '';
input.focus();
});
function resetGame() {
randomNumber = Math.floor(Math.random() * 10) + 1;
tries = 0;
message.textContent = 'Guess a number between 1 and 100';
input.value = '';
}
});