-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
92 lines (72 loc) · 1.85 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
var readlineSync = require("readline-sync");
var score = 0;
var questions = [{
question: "\nDuggu is my pet name 😁 Do you know my real name? ",
answer: "Divyanshu"
},
{
question: "\nHow old am I? ",
answer: "19"
},
{
question: "\nWhere do I live? ",
answer: "Ahmedabad"
},
{
question: "\nWhat am I currently learning? ",
answer: "JavaScript"
},
{
question: "\nThe best GURU I've ever got?",
answer: "Tanay Pratap"
},
{
question: "\nWhat do I like the most - Mountains or Beaches?",
answer: "Mountains"
}];
//If ans is correct this func is executed
function correct() {
console.log("Dammn, you are right!!");
score += 1;
console.log("\nCurrent Score: ", score, "\n");
console.log("-------------------");
}
//If ans is wrong this function is executed
function wrong(answer) {
console.log("Ohh snap!! You did a mistake");
console.log("Correct ans is ", answer);
if(score > 0){
score -= 1;
}else{
score = 0;
}
console.log("\nCurrent Score: ", score, "\n");
console.log("-------------------");
}
//The function will compare the original answer and answer given by player
function play(question, answer){
var userAnswer = readlineSync.question(question);
if(userAnswer.toLowerCase() === answer.toLowerCase()){
correct();
}else{
wrong(answer);
}
}
//This will loop through the questions
function game() {
for (var i=0; i<questions.length; i++) {
var currentQuestion = questions[i];
// console.log(currentQuestion)
play(currentQuestion.question, currentQuestion.answer);
}
}
function start() {
console.log("------------------------------");
var userName = readlineSync.question("What's your name? ");
console.log("Welcome " + userName + " Let's play a game!");
console.log("------------------------------");
game();
}
//Start the game
start();
console.log("Final Score: ", score);