-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
77 lines (53 loc) · 1.84 KB
/
script.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
function Question(question, answers, correct) {
this.question = question;
this.answers = answers;
this.correct = correct;
}
Question.prototype.display = function() {
console.log(this.question);
for(var i = 0; i < this.answers.length; i++) {
console.log(i + ': ' + this.answers[i]);
}
};
Question.prototype.checkAnswer = function(answer, callback) {
var sc;
if(answer === this.correct) {
console.log('Correct Answer!');
sc = callback(true);
}
else {
console.log('Wrong Answer! try again :)');
sc = callback(false);
}
this.displayScore(sc);
};
Question.prototype.displayScore = function(score) {
console.log('Your current score: ' + score);
console.log('------------------------------------------------------------');
};
function score() {
var sc = 0;
return function(correctAnswer) {
if(correctAnswer) {
sc++;
}
return sc;
};
}
var keepScore = score();
var q1 = new Question('What is the gaming tag of creator of this quiz?', ['Optimus', 'Goku', 'Saiyan'], 0);
var q2 = new Question('Is the creator a Saiyan?', ['No', 'Yes'], 1);
var q3 = new Question('Is the creator a frontend developer or a backend developer?', ['Frontend', 'Backend'], 1);
var q4 = new Question('How many organization is the creator in?', ['1', '2', '3', '4'], 3);
var q5 = new Question('What is the creator\'s favorite food item?', ['Kaju', 'Frankie', 'Both of them'], 0);
var questions = [q1, q2, q3, q4, q5];
function nextQuestion() {
var number = Math.floor(Math.random() * questions.length);
questions[number].display();
var answer = prompt("Please select the correct option.");
if(answer !== 'exit') {
questions[number].checkAnswer(parseInt(answer), keepScore);
nextQuestion();
}
}
nextQuestion();