-
Notifications
You must be signed in to change notification settings - Fork 0
/
9index.js
55 lines (55 loc) · 1.8 KB
/
9index.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
#!/usr/bin/env node
import inquirer from 'inquirer';
const questions = [
{
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 = {
totalQuestions: questions.length,
correctAnswers: correctAnswers,
};
showResult(quizResult);
};
const promptQuestion = async (questionNumber, question) => {
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) => {
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();