-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
75 lines (55 loc) · 1.42 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
var readlineSync = require("readline-sync");
var score = 0;
// data of high score
var highScores = [
{
name: "Tanay",
score: 3,
},
{
name: "Akash",
score: 2,
},
]
// array of objects
var questions = [{
question: "What's the full and original name of Captain America?",
answer: "Steve Rogers"
}, {
question: "What's Thor's hammer called?",
answer: "Mjolnir"
},
{
question: "Do you know what's Spiderman's real name?",
answer: "Peter Parker"
}];
function welcome() {
var userName = readlineSync.question("What's your name? ");
console.log("Welcome "+ userName + "let's see if you know Marvel Cinematic Universe");
}
// play function
function play(question, answer) {
var userAnswer = readlineSync.question(question);
if (userAnswer.toUpperCase() === answer.toUpperCase()) { // branching
console.log("right!");
score = score + 1;
} else {
console.log("wrong!");
}
console.log("current score: ", score);
console.log("-------------")
}
function game() {
for (var i=0; i<questions.length; i++) {
var currentQuestion = questions[i];
play(currentQuestion.question, currentQuestion.answer)
}
}
function showScores() {
console.log("YAY! You SCORED: ", score);
console.log("Check out the high scores, if you should be there ping me and I'll update it");
highScores.map(score => console.log(score.name, " : ", score.score))
}
welcome();
game();
showScores();