-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (91 loc) · 2.51 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
//import required packages
let readlineSync = require('readline-sync');
let chalk = require("chalk");
const log = console.log;
//initialize app variables
let userScore = 0;
let isUserBeatScore = false;
let questions = [
{
question: 'What is Anand\'s good name?',
answer: 'anand'
},
{
question: 'Where did Anand was born?',
answer: 'simdega'
},
{
question: 'Where does Anand live presently?',
answer: 'bhubaneswar'
},
{
question: 'Which sport is Anand\'s favourite..?',
answer: 'cricket'
},
{
question: 'what does Anand like to do in spare time..?',
answer: 'reading'
}
];
let highScores = [
{
name: "Raj",
score: 9
},
{
name: "Gorab",
score: 8
},
{
name: "Rahul",
score: 5
}
];
//welcome user
function welcome() {
let userName = readlineSync.question(chalk.white.bgBlue.bold('May i know your name please..?'));
log(chalk.magenta(`\nWelcome ${userName}, let's see how well DO YOU KNOW ANAND..?`));
//let user know rules
log(chalk.bgMagenta.bold("\nRules: "));
log(chalk.bgBlue("1. There are a total of 5 questions. All are compulsory."));
log(chalk.bgBlue("2. Each right answer will give you 2 points."));
log(chalk.bgBlue("3. 1 point will be deducted for each wrong answers.\n\n"));
readlineSync.questionInt(chalk.white.bgBlue.bold('Enter any number to start quiz!'));
console.clear();
}
//execute game
function play(question, answer) {
let userAnswer = readlineSync.question(chalk.bgBlue(question));
if (userAnswer.toLocaleLowerCase() === answer) {
userScore += 2;
log(chalk.green("\nYou were right!"));
} else {
userScore -= 1;
log(chalk.red("\nYou were wrong!"));
}
log("Your current score is " + userScore);
log(chalk.cyan("*************************\n"));
}
function game() {
for (let i = 0; i < questions.length; i++) {
let { question, answer } = questions[i];
play(question, answer);
}
}
function showScores() {
log(chalk.magenta.bgWhite.bold('Check out the high scores: '));
console.table(highScores);
highScores.forEach(({ name, score }) => {
if (userScore > score) isUserBeatScore = true;
});
log(chalk.bgMagenta("\n Yay! your highest score is " + userScore));
if (isUserBeatScore) {
log(chalk.green("\n Congrats! you made to the leaderboard. your name will be added to leaderboard."));
log(chalk.yellow("\nJust send me the screenshot of score!"));
} else {
log(chalk.red("\n Sorry!! you failed to beat the highest score!\n"));
}
}
welcome();
game();
showScores();