-
Notifications
You must be signed in to change notification settings - Fork 58
/
index.js
158 lines (133 loc) · 4.27 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
#!/usr/bin/env node
// /*
// /$$$$$$$$ /$$$$$$ / $$$$$$$ / $$$$$$$$ / $$$$$$ / $$ / $$ / $$$$$$ / $$$$$$$ / $$
// | $$_____/|_ $$_/| $$__ $$| $$_____/ /$$__ $$| $$ | $$|_ $$_/| $$__ $$|__/
// | $$ | $$ | $$ \ $$| $$ | $$ \__/| $$ | $$ | $$ | $$ \ $$ /$$ /$$$$$$
// | $$$$$ | $$ | $$$$$$$/| $$$$$ | $$$$$$ | $$$$$$$$ | $$ | $$$$$$$/| $$ /$$__ $$
// | $$__/ | $$ | $$__ $$| $$__/ \____ $$| $$__ $$ | $$ | $$____/ | $$| $$ \ $$
// | $$ | $$ | $$ \ $$| $$ /$$ \ $$| $$ | $$ | $$ | $$ | $$| $$ | $$
// | $$ /$$$$$$| $$ | $$| $$$$$$$$| $$$$$$/| $$ | $$ /$$$$$$| $$ /$$ | $$| $$$$$$/
// |__/ |______/|__/ |__/|________/ \______/ |__/ |__/|______/|__/|__/ |__/ \______/
// */
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';
let playerName;
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
async function welcome() {
const rainbowTitle = chalkAnimation.rainbow(
'Who Wants To Be A JavaScript Millionaire? \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 the questions right...
`);
}
async function handleAnswer(isCorrect) {
const spinner = createSpinner('Checking answer...').start();
await sleep();
if (isCorrect) {
spinner.success({ text: `Nice work ${playerName}. That's a legit answer` });
} else {
spinner.error({ text: `💀💀💀 Game over, you lose ${playerName}!` });
process.exit(1);
}
}
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;
}
function winner() {
console.clear();
figlet(`Congrats , ${playerName} !\n $ 1 , 0 0 0 , 0 0 0`, (err, data) => {
console.log(gradient.pastel.multiline(data) + '\n');
console.log(
chalk.green(
`Programming isn't about what you know; it's about making the command line look cool`
)
);
process.exit(0);
});
}
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');
}
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"');
}
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');
}
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', // Correct
],
});
return handleAnswer(answers.question_4 === 'object');
}
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', 'synchronous', 'promise-based'],
});
return handleAnswer(answers.question_5 === 'non-blocking');
}
// Run it with top-level await
console.clear();
await welcome();
await askName();
await question1();
await question2();
await question3();
await question4();
await question5();
winner();