-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
246 lines (210 loc) Β· 7.23 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#!/usr/bin/env node
/* Importing the modules. */
import chalk from "chalk";
import inquirer from "inquirer";
import gradient from "gradient-string";
import chalkAnimation from "chalk-animation";
import figlet from "figlet";
import { createSpinner } from "nanospinner";
import updateNotifier from "simple-update-notifier";
import packageJson from "./package.json" assert { type: "json" };
/* It's checking to see if there's a newer version of the package available. */
updateNotifier({
pkg: packageJson,
});
/* Declaring a variable. */
let playerName;
/**
* Sleep is a function that returns a promise that resolves after a given number of milliseconds.
* @param [ms=2000] - The number of milliseconds to wait before resolving the promise.
*/
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
/**
* It's a function that returns a promise that resolves after a certain amount of time.
*/
async function welcome() {
const rainbowTitle = chalkAnimation.rainbow(
"Who wants to be a JavaScript Millioaire? \n"
);
await sleep();
rainbowTitle.stop();
console.log(`
${chalk.bgBlue("HOW TO PLAY!")}
I am a process on your computer.
If you get any question wrong I will be ${chalk.bgRed("killed")}
So get all questions right...
`);
}
/* It's calling the `welcome()` function, and then waiting for it to finish before continuing. */
await welcome();
/**
* It asks the user for their name, and then sets the variable playerName to the answer.
* @returns The player's name
*/
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;
}
/* It's calling the `askName()` function, and then waiting for it to finish before continuing. */
await askName();
/**
* The function is called question1, it's an async function, it uses the inquirer package to ask a
* question, it returns the result of the handleAnswer function, which is passed a boolean value.
*
* The handleAnswer function is defined as follows:
*
* function handleAnswer(isCorrect) {
* if (isCorrect) {
* console.log('Correct!')
* } else {
* console.log('Incorrect!')
* }
* }
*/
async function question1() {
const answers = await inquirer.prompt({
name: "question_1",
type: "list",
message: "JavaScript was created in 10 days then released on\n",
choices: [
"May 23rd, 1995",
"Nov 24th, 1995",
"Dec 4th, 1995",
"Dec 17, 1996",
],
});
return handleAnswer(answers.question_1 === "Dec 4th, 1995");
}
/* It's calling the `question1()` function, and then waiting for it to finish before continuing. */
await question1();
/**
* It asks the user a question, and if the user answers correctly, it returns true. Otherwise, it
* returns false.
*/
async function question2() {
const answers = await inquirer.prompt({
name: "question_2",
type: "list",
message: 'What is x? var x = 1_1 + "1" + Number(1)\n',
choices: ["4", '"4"', '"1111"', "69420"],
});
return handleAnswer(answers.question_2 === "1111");
}
/* It's calling the `question2()` function, and then waiting for it to finish before continuing. */
await question2();
/**
* The function asks the user a question, and if the user's answer is correct, it returns true.
* Otherwise, it returns false.
*/
async function question3() {
const answers = await inquirer.prompt({
name: "question_3",
type: "list",
message: `What is the first element in the array? ['π', 'π¦', 'π'].length = 0\n`,
choices: ["0", "π", "π", "undefined"],
});
return handleAnswer(answers.question_3 === "undefined");
}
/* It's calling the `question3` function, and then waiting for it to finish before continuing. */
await question3();
/**
* It asks the user a question, and returns a boolean value based on whether or not the user answered
* correctly.
* @returns true or false
*/
async function question4() {
const answers = await inquirer.prompt({
name: "question_4",
type: "list",
message: "Which of the following is NOT a primitive type?\n",
choices: ["boolean", "number", "null", "object"],
});
return handleAnswer(answers.question_4 === "object");
}
/* It's calling the `question4()` function, and then waiting for it to finish before continuing. */
await question4();
/**
* It asks the user a question, and returns a boolean value based on the answer.
*/
async function question5() {
const answers = await inquirer.prompt({
name: "question_5",
type: "list",
message:
"JS is a high-level single-threaded, garbage-collected,\n" +
"interpreted(or just-in-time compiled), prototype-based,\n" +
"multi-paradigm, dynamic language with a ____ event loop\n",
choices: ["multi-threaded", "non-blocking", "synchronos", "promise-based"],
});
return handleAnswer(answers.question_5 === "non-blocking");
}
/* It's calling the `question5()` function, and then waiting for it to finish before continuing. */
question5();
/**
* "This function asks the user a question, and then returns the result of the handleAnswer function,
* which takes a boolean as an argument."
*/
async function question6() {
const answers = await inquirer.prompt({
name: "question_6",
type: "list",
message: "Who Designed JavaScript?\n",
choices: ["Brendan Eich", "Eich Brendan", "None of the above"],
});
return handleAnswer(answers.question_6 === "Brenden Eich");
}
/* It's calling the `question6()` function, and then waiting for it to finish before continuing. */
question6();
/**
* The function takes a boolean value as an argument and returns a promise.
* @param isCorrect - A boolean value that indicates whether the player's answer was correct.
*/
async function handleAnswer(isCorrect) {
const spinner = createSpinner("Checking Your Answer....").start();
await sleep();
if (isCorrect) {
spinner.success({
text: `Nice work ${playerName}. This is correct Answer.`,
});
} else {
spinner.error({
text: `Game Over, you lose ${playerName}!`,
});
process.exit(1);
}
}
/**
* It takes a string, and then prints it out in a big font.
*
* The function is called `winner()` because it's going to be used to congratulate the player when they
* win the game.
*
* The first line of the function is a comment. It's a reminder to myself that this function is going
* to be used to congratulate the player when they win the game.
*
* The second line is a call to the `console.clear()` function. This function clears the console. It's
* used here to make sure that the congratulations message is the only thing that's printed to the
* console.
*
* The third line is a variable declaration. The variable is called `msg`. It's a string that contains
* the message that will be printed to the console.
*
* The fourth line is a call to the `figlet` function. This function takes two arguments: a string, and
* a callback
*/
function winner() {
console.clear();
const msg = `Congrats, ${playerName}!\n Here's your reward of 1,000,000$`;
figlet(msg, (err, data) => {
console.log(gradient.pastel.multiline(data));
});
}
/* It's printing a message to the console. */
winner();