-
Notifications
You must be signed in to change notification settings - Fork 4
/
practice6.js
31 lines (25 loc) · 1.25 KB
/
practice6.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
let number = Math.floor((Math.random() * 100) + 1); // Generates a random number b/w 1 and 100
let chances = 0; // Initial vaulue of chances taken to guess a number
let guess; // Variable to store the number to be input from user to be guessed
console.log("This is a guessing game and you have to enter a number and if it matches with the number generated by computer, then you win...\n");
guess = prompt("Enter a number between 1 and 100");
++chances; // To increment the number of chances taken
do {
guess = Number.parseInt(guess);
if (guess > number) {
console.log("Number entered is greater");
guess = prompt("Enter again!!");
guess = Number.parseInt(guess);
++chances; // To increment the number of chances taken
continue;
}
else if (guess < number) {
console.log("Number entered is smaller");
guess = prompt("Enter again!!");
guess = Number.parseInt(guess);
++chances; //To increment the number of chances taken
continue;
}
} while (guess != number); //break the loop if number entered is equal to the number generated
let score = 100 - chances; //To store the score of your game
console.log("\nCongratulations🥳🥳\nThe number generated was", number + " and you guessed it right😁\nYour final score is", score);