-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
57 lines (55 loc) · 1.41 KB
/
index.ts
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
53
54
55
56
57
import inquirer from "inquirer";
import chalk from "chalk";
console.log(
chalk.bgYellow.black(
"Welcome to the Number Guessing Game! Guess a number between 1 and 100."
)
);
askForGuess();
function askForGuess() {
const secretNumber = Math.floor(Math.random() * 101);
inquirer
.prompt([
{
type: "input",
name: "guess",
message: chalk.blue("Enter your guess:"),
validate: (input) => {
if (input > 100 || input < 1) {
return "Please enter a number from 1 to 100";
} else if (isNaN(input)) {
return "Please enter a valid number.";
}
return true;
},
},
])
.then((answers) => {
if (parseInt(answers.guess) === secretNumber) {
console.log(
chalk.green(
`Congratulations! You've guessed the secret number: ${secretNumber}.`
)
);
} else {
console.log(
chalk.red(`Wrong! The correct secret number is: ${secretNumber}`)
);
}
inquirer
.prompt([
{
type: "confirm",
name: "playAgain",
message: chalk.yellow("Do you want to play again?"),
},
])
.then((answers) => {
if (answers.playAgain) {
askForGuess();
} else {
console.log(chalk.bgCyan("Thanks for playing!"));
}
});
});
}