-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
32 lines (24 loc) · 1019 Bytes
/
script.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
// Generate a number between 1 and 100
const targetNumber = Math.floor(Math.random()*100)+1;
let attempts = 0;
function checkGuess() {
const guessInput = document.getElementById('guessInput');
const message = document.getElementById('message');
const attemptsSpan = document.getElementById('attempts');
const userGuess = parseInt(guessInput.value);
if (isNaN(userGuess) || userGuess < 1 || userGuess >100) {
message.textContent = 'Please enter a valid number between 1 and 100';
return;
}
attempts++;
if(userGuess === targetNumber) {
message.textContent = `Congratulations! You guessed the correct number in ${attempts} attempts.`;
message.style.color = 'green';
guessInput.disabled = true;
} else {
const hint = userGuess < targetNumber ? 'higher' : 'lower';
message.textContent = `Try again! The correct number is ${hint}.`;
message.style.color = 'red'
}
attemptsSpan.textContent = attempts;
}