-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
179 lines (148 loc) · 4.89 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import chalk from "chalk";
import readlinesync from 'readline-sync'
var highScores = [
{
"Name": "Asish",
"Score": 9
},
{
"Name": "Subhasish",
"Score": 8
},
{
"Name": "Vishal",
"Score": 7
}
]
function WelcomeScreen() {
console.log(chalk.blue('Welcome player \n'))
var playerName = readlinesync.question("Please enter your name \n");
console.log(chalk.blueBright('Welcome ' + playerName))
var playerChoice = readlinesync.question(chalk.red("Enter 1 to begin game and 2 to view current highest scores and 3 to exit the game \n"))
if (playerChoice == 2) {
showLeaderBoard()
} else if (playerChoice == 1) {
playGame(playerName)
}
else {
return
}
}
function showLeaderBoard() {
for (var i = 0; i < highScores.length; i++) {
console.log(chalk.magentaBright(i + 1 + " " + highScores[i].Name + " - " + highScores[i].Score))
}
}
function playGame(playerName) {
var score = Play()
checkHighScores(playerName, score)
}
//List of Questions
function Play() {
var score = 0;
console.log("Starting quiz......\n")
var questions = [
{//1
"question": "Who is my favorite Superhero?\n",
"options": ["1. The Flash", "2. Batman", "3. Iron man", "4. The Hulk"],
"correctAnswer": "1. The Flash",
},
{//2
"question": "What is my favorite comfort food?\n",
"options": ["1. Instant ramen ", "2. Cupcakes ", "3. Pizza ", "4. Noodles"],
"correctAnswer": "1. Instant ramen "
},
{//3
"question": "What is my favorite thing to do while bored?\n",
"options": ["1. Listening to music ", "2. Playing videogames ", "3. Going on long drives ", "4. Reading books "],
"correctAnswer": "2. Playing videogames "
},
{//4
"question": "What is my favorite single player videogame?\n",
"options": ["1. GTA V ", "2. Elden Ring ", "3. Mortal Kombat ", "4.The Witcher"],
"correctAnswer": "2. Elden Ring "
},
{//5
"question": "what is my favorite programming language?\n",
"options": ["1. Go ", "2. C# ", "3. Ruby ", "4. Java"],
"correctAnswer": "2. C# "
},
{//6
"question": "what is my favorite Multiplayer game?\n",
"options": ["1. Valorant ", "2. Rocket League ", "3. GTA Online ", "4. Apex"],
"correctAnswer": "2. Rocket League "
},
{//7
"question": "what is my favorite tech company?\n",
"options": ["1. Apple ", "2. Samsung ", "3. Sony ", "4. Google"],
"correctAnswer": "3. Sony "
},
{//8
"question": "What is my favorite sitcom show?\n",
"options": ["1. FRIENDS ", "2. Man With a Plan ", "3. Two and a Half Men ", "4. Big Bang Theory"],
"correctAnswer": "1. FRIENDS "
},
{//9
"question": "Who is my favorite Artist?\n",
"options": ["1. Charlie Puth ", "2. The Weeknd ", "3. Taylor Swift ", "4. NF"],
"correctAnswer": "2. The Weeknd "
},
{//10
"question": "What type of a person I am? A dog person or a cat person?\n",
"options": ["1. Dog person ", "2. Cat person"],
"correctAnswer": "2. Cat person"
},
]
console.log("Your current Score is " + score);
for (var i = 0; i < questions.length; i++) {
console.log(questions[i].question)
console.log(...questions[i].options)
console.log()
var userAnswer = readlinesync.question("Pick between [1-4] - ")
if (questions[i].options[userAnswer - 1] == questions[i].correctAnswer) {
console.log()
score++
console.log(chalk.green("Correct Answer!"))
console.log()
console.log(chalk.yellowBright("Your score is " + score))
console.log("--------------------------\n")
}
else {
console.log()
console.log(chalk.red('Wrong Answer'))
console.log()
console.log(chalk.yellowBright("Your score is " + score))
console.log("--------------------------\n")
}
}
return score;
}
function checkHighScores(playerName, score) {
var [currentMaxScore, currentMaxScorerName, index] = getCurrentHighScore()
if (score > currentMaxScore) {
console.log(chalk.green('Congratulations!! ' + playerName + ' You beat ' + currentMaxScorerName + " to have the highest score in the game.\n"))
highScores[index].Name = playerName
highScores[index].Score = score
showLeaderBoard()
console.log(chalk.redBright("Please contact the admin to permanetly update the leaderboard with your new score.\n"))
console.log("Thank you for playing.")
}
else {
console.log(chalk.blueBright("Your final Score is " + score))
console.log(chalk.blue("Thank you for playing."))
}
}
function getCurrentHighScore() {
var highscore = highScores[0].Score
var name = highScores[0].Name
var index = 0
for (var i = 0; i < highScores.length; i++) {
if (highScores[i].Score > highscore) {
highscore = highScores[i].Score
name = highScores[i].Name
index = i
}
}
return [highscore, name, index]
}
WelcomeScreen()