-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge7.js
More file actions
59 lines (48 loc) · 1.32 KB
/
Copy pathchallenge7.js
File metadata and controls
59 lines (48 loc) · 1.32 KB
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
(function() {
var score = 0;
var contin = true;
function Question(question, choices, correct) {
this.question = question;
this.choices = choices;
this.correct = correct;
}
Question.prototype.display = function() {
console.log(this.question);
for (var i = 0; i < this.choices.length; i++) {
console.log(i + ': ' + this.choices[i]);
}
var answer = prompt('What is the answer?');
var res = check(answer, this.correct);
while (res) {
answer = prompt('What is the answer?');
res = check(answer, this.correct);
}
};
var q1 = new Question('What is 1?', [1,2], '0');
var q2 = new Question('What is 2?', [1,2,3,4,5,6], '1');
var q3 = new Question('What is 3?', [1,2,3], '2');
var q4 = new Question('What is 4?', [1,2,3,4], '3');
var questions = [q1, q2, q3, q4];
function ask(questions) {
var current = Math.floor(Math.random() * questions.length);
questions[current].display();
}
function check(answer, correct) {
if (answer === 'exit') {
contin = false;
return false
} else if (answer === correct) {
console.log('Correct!!!');
score++;
console.log('Your score is: ' + score);
console.log('-------------------------------------------');
return false;
} else {
console.log('Wrong :( ');
return true;
}
}
while (contin) {
ask(questions);
}
})();