-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
106 lines (94 loc) · 2.63 KB
/
index.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env node
import chalk from "chalk";
import inquirer from "inquirer";
import chalkAnimation from "chalk-animation";
import gradient from "gradient-string";
import figlet from "figlet";
import { createSpinner } from "nanospinner";
let playerName;
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
async function welcome() {
const rainbowTitles = chalkAnimation.rainbow(" Food Quiz! \n");
await sleep();
rainbowTitles.stop();
console.log(`
${chalk.bgGreen("HOW TO PLAY")}
I'm a process in your computer
If you get any question wrong I will be ${chalk.bgRed("killed")}
`);
}
async function askName() {
const answers = await inquirer.prompt({
name: "player_name",
type: "input",
message: "What is your name?",
default() {
return "Player";
},
});
playerName = answers.player_name;
}
async function question1() {
const answers = await inquirer.prompt({
name: "question1",
type: "list",
message: "Rice is originally from: ",
choices: ["Europe", "America", "Africa", "Asia"],
});
return handleAnswer(answers.question1 == "Asia");
}
async function question2() {
const answers = await inquirer.prompt({
name: "question2",
type: "list",
message: "Which of these food is known to be French: ",
choices: ["hamburguer", "beans", "croissant", "pasta"],
});
return handleAnswer(answers.question2 == "croissant");
}
async function question3() {
const answers = await inquirer.prompt({
name: "question3",
type: "list",
message: "Which of these is a citrus fruit: ",
choices: ["apple", "orange", "grapes", "banana"],
});
return handleAnswer(answers.question3 == "orange");
}
async function question4() {
const answers = await inquirer.prompt({
name: "question4",
type: "list",
message: `Pasta is originally from: \n ${chalk.bgRed(
"Not what you think!",
)}`,
choices: ["chinese", "italian", "brazillian", "spanish"],
});
return handleAnswer(answers.question4 == "chinese");
}
async function handleAnswer(isCorrect) {
const spinner = createSpinner("Check answer...").start();
await sleep();
if (isCorrect) {
spinner.success({
text: `Nice work, ${playerName}. That's the right answer`,
});
} else {
spinner.error({ text: `Game Over, you lose ${playerName}!` });
process.exit(1);
}
}
function winner() {
console.clear();
const msg = `Congrats, ${playerName}! You won the game.`;
figlet(msg, (err, data) => {
console.log(gradient.pastel.multiline(data));
});
}
await welcome();
await askName();
await question1();
await question2();
await question3();
await question4();
winner();