-
Notifications
You must be signed in to change notification settings - Fork 0
/
27. Guess_a_random_number.js
52 lines (43 loc) · 1.6 KB
/
27. Guess_a_random_number.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//*----- Guess a random number
//? This program generates a random number between a specified range and allows user to guess the number.It provides feedback on whether the guessed number is too high,too low or correct.The user continues guessing untill they correctly identify the random number.
// Function to start the number guessing game
function numberGuessingGame() {
// Define the range for the random number
let lowerBound = parseInt(prompt("Enter the lower bound of the range:"), 10);
let upperBound = parseInt(prompt("Enter the upper bound of the range:"), 10);
// Validate the range
if (isNaN(lowerBound) || isNaN(upperBound) || lowerBound >= upperBound) {
alert(
"Invalid range. Please ensure that the lower bound is less than the upper bound."
);
return;
}
const randomNumber =
Math.floor(Math.random() * (upperBound - lowerBound + 1)) + lowerBound;
let attempts = 0;
let guess;
do {
// Prompt user to guess the number
guess = parseInt(
prompt(`Guess a number between ${lowerBound} and ${upperBound}:`),
10
);
attempts++;
if (isNaN(guess)) {
alert("Please enter a valid number.");
attempts--; // Do not count invalid input as an attempt
continue;
}
if (guess < randomNumber) {
alert("Too low! Try again.");
} else if (guess > randomNumber) {
alert("Too high! Try again.");
} else {
alert(
`Congratulations! You've guessed the number ${randomNumber} correctly in ${attempts} attempts.`
);
}
} while (guess !== randomNumber);
}
// Start the game
numberGuessingGame();