-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (79 loc) · 2.8 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
const chalk = require('chalk');
const readlineSync = require('readline-sync');
var totalScore = 0;
var questionData =[
{
question: "Where does Varun work currently? ",
options: ["Microsoft", "Amazon", "PayPal", "Google"],
answer: 2
},
{
question: "Which programming language is Varun's favorite? ",
options: ["Java", "Python", "C++", "JavaScript"],
answer: 1
},
{
question: "Where does Varun live currently? ",
options: ["Allahabad", "Gurgaon", "Bangalore", "Chennai", "Delhi"],
answer: 0
},
{
question: "Which programming language Varun is learning currently? ",
options: ["Java", "Python", "C++", "JavaScript"],
answer: 3
},
{
question: "What is Varun's favorite dessert? ",
options: ["Gulab Jamun", "Ras Malai", "Paan Kulfi", "Kaju Katli", "All of these"],
answer: 4
}
]
function checkQuestion(question, options, answer){
correctAnswerMsg = [
"That's correct!",
"You are right!! ;)",
"Yoo-hoo, it is the right answer",
'Looks like you know this!!'
];
incorrectAnswerMsg = [
"I am sorry, but you are wrong",
"Looks like you don't know this!! :/",
"That's incorrect :(",
"Better luck next time"
];
var correctAnswerIndex = Math.floor(Math.random()*correctAnswerMsg.length);
var incorrectAnswerIndex = Math.floor(Math.random()*incorrectAnswerMsg.length);
console.log(chalk.blueBright.bold(question));
userAnswer = readlineSync.keyInSelect(options, ":", {cancel:"I don't know"});
if(userAnswer === -1 ){
console.log(chalk.whiteBright.italic("No worries, talk to Varun to know more about him ;)"));
}
else if(answer === userAnswer){
console.log(chalk.greenBright(correctAnswerMsg[correctAnswerIndex]))
totalScore++;
} else {
console.log(chalk.redBright(incorrectAnswerMsg[incorrectAnswerIndex]))
}
console.log(chalk.cyan.bold("---------------------------------------"));
}
function shuffle(array) {
var currentIndex = array.length, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex--;
// And swap it with the current element.
[array[currentIndex], array[randomIndex]] = [
array[randomIndex], array[currentIndex]];
}
return array;
}
console.log(chalk.cyan.bold("Welcome to this quiz!!!"))
console.log(chalk.cyan.bold("It is a test of your friendship with Varun Tandon"))
console.log(chalk.cyan.bold("---------------------------------------"));
questionData = shuffle(questionData);
for(var i = 0; i < questionData.length; i++){
checkQuestion(questionData[i].question, questionData[i].options, questionData[i].answer )
}
console.log(chalk.cyan.bold("Your final score is "+ totalScore+"!!! "))