-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
87 lines (71 loc) · 2 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
#!/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'
import { question } from './questions.js'
let correctAnswers = 0
const sleep = (ms = 1000) => new Promise((r) => setTimeout(r, ms))
async function welcome() {
figlet(`Welcome to :`, (_, data) => {
console.log(gradient.pastel.multiline(data) + '\n')
});
await sleep()
figlet(`The Quiz !\n`, (_, data) => {
console.log(gradient.pastel.multiline(data) + '\n')
});
await sleep(1000)
console.log(`
${chalk.bgYellow.black(' Use arrow keys ')}
${chalk.bgGreen.black(' Or Vim motions ')}
`);
await sleep(2500)
}
async function handleAnswer(isCorrect) {
const spinner = createSpinner('Loading...').start()
await sleep()
if (isCorrect) {
correctAnswers++
spinner.success({ text: `Correct!` })
} else {
console.log('\n❌❌❌ You are wrong!')
figlet(`You loose!`, (_, data) => {
console.log(gradient.morning.multiline(data) + '\n')
console.log(
chalk.redBright(
` ${correctAnswers} out of ${question.length} answers are correct`
)
)
process.exit(0)
})
process.exit(1)
}
}
async function winner() {
console.clear()
figlet(`You win!`, (_, data) => {
console.log(gradient.pastel.multiline(data) + '\n')
})
await sleep()
chalkAnimation.rainbow(` ${correctAnswers} out of ${question.length} answers are correct`)
await sleep(1500)
process.exit(0)
}
async function triggerQuestions() {
for (let i = 0; i <= question.length - 1; i++) {
const answers = await inquirer.prompt({
name: question[i].name,
type: 'list',
message: question[i].message,
choices: question[i].choices,
})
handleAnswer(Object.values(answers).toString() === question[i].answer)
await sleep()
}
}
console.clear()
await welcome()
await triggerQuestions()
winner()