-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
43 lines (36 loc) · 930 Bytes
/
main.js
File metadata and controls
43 lines (36 loc) · 930 Bytes
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
const quiz = [
{name: "Superman", realName: "Clark Kent"},
{name: "Wonder Woman", realName: "Diana Prince"},
{name: "Batman", realName: "Bruce Wayne"},
];
const game = {
start(quiz) {
this.questions = [...quiz];
this.score = 0;
// main game loop
for (const question of this.questions) {
this.question = question;
this.ask();
}
// end of main game loop
this.gameOver();
},
ask() {
const question = `What is ${this.question.name}'s real name?`;
const response = prompt(question);
this.check(response);
},
check(response) {
const answer = this.question.realName;
if (response === answer) {
alert('Correct!');
this.score++;
} else {
alert(`Wrong! The correct answer was ${answer}`);
}
},
gameOver() {
alert(`Game Over, you scored ${this.score} point${this.score !== 1 ? 's' : ''}`);
}
};
game.start(quiz);