-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
149 lines (135 loc) · 3.94 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
var readlineSync = require("readline-sync");
var chalk = require("chalk");
var gradient = require("gradient-string");
var userScore = 0;
var highScores = [
{
name: "Shivam",
score: 5,
},
{
name: "Kokila",
score: 4,
},
{
name: "Satyam",
score: 3,
},
];
var questionOne = {
type: "yes/no",
question: "Am I born & brought up in Lucknow? ",
answer: true,
};
var questionTwo = {
type: "yes/no",
question: "Am I over 25 years old? ",
answer: false,
};
var questionThree = {
type: "multiple choice",
question: "What's the name of first school I attended? ",
options: ["Tender Hearts", "St.Antony's", "Happy World", "Nurture Nest"],
answer: "Happy World",
};
var questionFour = {
type: "yes/no",
question: "Have I graduated yet? ",
answer: false,
};
var questionFive = {
type: "yes/no",
question: "Have I joined neoG Level 1 before? ",
answer: true,
};
var questions = [
questionOne,
questionTwo,
questionThree,
questionFour,
questionFive,
];
function printCurrentScore() {
console.log(chalk.hex("#FDFF00")("Your Score: " + userScore));
}
function ask(type, question, correctAnswer, options = null) {
var userAnswer;
if (type === "yes/no") {
userAnswer = readlineSync.keyInYNStrict(["\nQ. " + question]);
} else if (type === "multiple choice") {
var userAnswerIndex = readlineSync.keyInSelect(options, "Q. " + question, {
cancel: false,
});
userAnswer = options[userAnswerIndex];
}
if (userAnswer === correctAnswer) {
userScore++;
console.log(chalk.hex("9CFF2E")("That's right!"));
} else {
console.log(chalk.hex("#EF4040")("Wrong!"));
}
printCurrentScore();
}
function printHighScores() {
console.log(
"\nHere's how the " + gradient.rainbow("high scores") + " look for now: "
);
for (
var highScoreIndex = 0;
highScoreIndex < highScores.length;
highScoreIndex++
) {
var highScorerInfo = highScores[highScoreIndex];
var highScorer = highScorerInfo.name;
var highScore = highScorerInfo.score;
console.log(gradient.rainbow(highScorer + " - " + highScore));
}
}
console.log(
chalk.grey.italic(
"Please know that this quiz is based on facts about me(Shivam Tewari); And is a fair test for anyone who's acquainted with me."
)
);
console.log("\n" + chalk.underline("Welcome to the MEmory Quiz(v2023)!"));
var userName = readlineSync.question("\nMay I have your name please: ");
var firstAlphabetOfUserName = userName.charAt(0);
firstAlphabetOfUserName = firstAlphabetOfUserName.toUpperCase();
var remainingUserName = userName.substring(1);
remainingUserName = remainingUserName.toLowerCase();
userName = firstAlphabetOfUserName + remainingUserName;
console.log("\nWelcome, " + chalk.hex("#2192FF")(userName) + "!");
readlineSync.question("Press the Enter key when you're ready to start...");
console.log();
userScore = 0;
var userIsAHighScorer = false;
for (var questionIndex = 0; questionIndex < questions.length; questionIndex++) {
var questionType = questions[questionIndex].type;
var question = questions[questionIndex].question;
var correctAnswer = questions[questionIndex].answer;
if (questionType === "multiple choice") {
var options = questions[questionIndex].options;
ask(questionType, question, correctAnswer, options);
continue;
}
ask(questionType, question, correctAnswer);
}
console.log(
"\n" +
chalk.hex("#FFA500")("You've scored a total of " + userScore + " points.")
);
var lowestHighScore = highScores[highScores.length - 1].score;
if (userScore >= lowestHighScore) {
userIsAHighScorer = true;
}
printHighScores();
if (userIsAHighScorer) {
console.log(
"\n🎊 Great Job on the quiz, " + chalk.hex("#2192FF")(userName) + " 🎊"
);
console.log(
"You've made it to the top! Send me a screenshot of your score at my email: www.shivamtewari@gmail.com to get the list updated."
);
}
console.log(
"\nThank You so much for attempting the quiz.\nHope you enjoyed learning a bit more about me."
);