-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCodingChallengeExpert.js
96 lines (73 loc) · 2.48 KB
/
CodingChallengeExpert.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
/*
Challenge Description : =>
--- Expert level ---
8. After you display the result, display the next random question, so that the game never ends (Hint: write a function for this and call it right after displaying the result)
9. Be careful: after Task 8, the game literally never ends. So include the option to quit the game if the user writes 'exit' instead of the answer. In this case, DON'T call the function from task 8.
10. Track the user's score to make the game more fun! So each time an answer is correct, add 1 point to the score (Hint: I'm going to use the power of closures for this, but you don't have to, just do this with the tools you feel more comfortable at this point).
11. Display the score in the console. Use yet another method for this.
*/
(function () {
function Question(question, answers, correct) {
this.question = question;
this.answers = answers;
this.correct = correct;
}
Question.prototype.displayQuestion = function () {
console.log(this.question);
for (var i = 0; i < this.answers.length; i++) {
console.log(i + ": " + this.answers[i]);
}
};
Question.prototype.checkAnswer = function (ans, callback) {
var sc;
if (ans === 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 is: " + score);
console.log("------------------------------");
};
var question1 = new Question(
"Is javaScript the coolest programming language in the world?",
["Yes", "No"],
0
);
var question2 = new Question(
"What is the name of this course's teacher?",
["John", "Micheal", "Jonas"],
2
);
var question3 = new Question(
"What does best describe coding?",
["Boring", "Hard", "Fun", "Tediuos"],
2
);
var questions = [question1, question2, question3];
// Closures
function score() {
var sc = 0;
return function (correct) {
if (correct) {
sc++;
}
return sc;
};
}
var keepScore = score();
function nextQuestion() {
var n = Math.floor(Math.random() * questions.length);
questions[n].displayQuestion();
var answer = prompt("Please select the correct answer.");
if (answer !== "exit") {
questions[n].checkAnswer(parseInt(answer), keepScore);
nextQuestion();
}
}
nextQuestion();
})();