-
Notifications
You must be signed in to change notification settings - Fork 0
/
random6.js
32 lines (28 loc) · 1.2 KB
/
random6.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
let randomNumber = Math.round(Math.random() * 100);
let counter = 6;
function userInput(prompt) {
let readlineSync = require('readline-sync');
let input = readlineSync.question(prompt);
return input;
}
let userNumber = Math.round(userInput("Enter a whole number between 1 and 100: "));
for (let i = 0; i < 5; i++) {
if (parseInt(userNumber) > randomNumber) {
console.log("Too big! Try again.");
counter -= 1;
userNumber = Math.round(userInput(`Remaining attempts: ${counter}.\nEnter a whole number between 1 and 100: `));
} else if (parseInt(userNumber) < randomNumber) {
console.log("Too small! Try again.");
counter -= 1;
userNumber = Math.round(userInput(`Remaining attempts: ${counter}.\nEnter a whole number between 1 and 100: `));
} else if (parseInt(userNumber) === randomNumber) {
console.log("You guessed it! Congratulations!");
return randomNumber;
} else {
console.log("Please enter a whole number between 1 and 100.");
userNumber = Math.round(userInput(`Remaining attempts: ${counter}.\nEnter a whole number between 1 and 100: `));
}
}
if (userNumber != randomNumber) {
console.log(`The random number was ${randomNumber}. Better luck next time!`);
}