-
Notifications
You must be signed in to change notification settings - Fork 0
/
9index.ts
77 lines (64 loc) · 1.98 KB
/
9index.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/env node
import inquirer from 'inquirer';
type Question = {
question: string;
options: string[];
correctAnswerIndex: number;
};
type QuizResult = {
totalQuestions: number;
correctAnswers: number;
};
const questions: Question[] = [
{
question: "What is the capital of France?",
options: ["Paris", "London", "Berlin", "Madrid"],
correctAnswerIndex: 0,
},
{
question: "Which planet is known as the Red Planet?",
options: ["Venus", "Mars", "Jupiter", "Saturn"],
correctAnswerIndex: 1,
},
{
question: "What is the largest mammal?",
options: ["Elephant", "Giraffe", "Blue Whale", "Hippopotamus"],
correctAnswerIndex: 2,
},
];
const main = async () => {
console.log("Welcome to the Quiz!\n");
let correctAnswers = 0;
for (const [index, question] of questions.entries()) {
const answer = await promptQuestion(index + 1, question);
if (answer === question.correctAnswerIndex) {
correctAnswers++;
}
}
const quizResult: QuizResult = {
totalQuestions: questions.length,
correctAnswers: correctAnswers,
};
showResult(quizResult);
};
const promptQuestion = async (questionNumber: number, question: Question): Promise<number> => {
const questionPrompt = `${questionNumber}. ${question.question}`;
const options = question.options;
const answer = await inquirer.prompt([
{
type: 'list',
name: 'answer',
message: questionPrompt,
choices: options,
},
]);
return options.indexOf(answer.answer);
};
const showResult = (quizResult: QuizResult) => {
console.log("\nQuiz Result:");
console.log(`Total Questions: ${quizResult.totalQuestions}`);
console.log(`Correct Answers: ${quizResult.correctAnswers}`);
console.log(`Incorrect Answers: ${quizResult.totalQuestions - quizResult.correctAnswers}`);
console.log(`Percentage: ${(quizResult.correctAnswers / quizResult.totalQuestions) * 100}%`);
};
main();