-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathquiz.js
More file actions
103 lines (73 loc) · 2.64 KB
/
quiz.js
File metadata and controls
103 lines (73 loc) · 2.64 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
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
97
98
99
100
101
102
103
class Quiz {
// YOUR CODE HERE:
constructor(questions,timeLimit,timeRemaining) {
this.questions = [
new Question("What is 2 + 2?", ["3", "4", "5", "6"], "4", 1),
new Question("What is the capital of France?", ["Miami", "Paris", "Oslo", "Rome"], "Paris", 1),
new Question("Who created JavaScript?", ["Plato", "Brendan Eich", "Lea Verou", "Bill Gates"], "Brendan Eich", 2),
new Question("What is the mass–energy equivalence equation?", ["E = mc^2", "E = m*c^2", "E = m*c^3", "E = m*c"], "E = mc^2", 3),
];
// 1. constructor (questions, timeLimit, timeRemaining)
this.questions = questions;
this.timeLimit = timeLimit;
this.timeRemaining = timeRemaining;
this.correctAnswers = 0;
this.currentQuestionIndex = 0;
}
// 2. getQuestion()
getQuestion() {
return this.questions[this.currentQuestionIndex];
}
// 3. moveToNextQuestion()
moveToNextQuestion() {
this.currentQuestionIndex++;
}
/* if (this.currentQuestionIndex < this.questions.length - 1) {
this.currentQuestionIndex++;
return this.currentQuestionIndex;
} else {
return "you\'re done";
} */
// 4. shuffleQuestions()
shuffleQuestions() {
let currentIndex = this.questions.length - 1;
while (currentIndex > 0) {
let randomIndex = Math.floor(Math.random() * (currentIndex + 1));
[this.questions[currentIndex], this.questions[randomIndex]] =
[this.questions[randomIndex], this.questions[currentIndex]];
currentIndex--;
}
/* return this.questions; */
}
// 5. checkAnswer(answer)
checkAnswer(answer) {
if (answer === this.getQuestion().answer) {
this.correctAnswers++;
}
}
// 6. hasEnded()
hasEnded() {
return this.currentQuestionIndex >= this.questions.length;
}
/* if (this.currentQuestionIndex < this.questions.length) {
return false;
}
else if (this.currentQuestionIndex == this.questions.length) {
return true;
}
} */
// 7. filterQuestionsByDifficulty(difficulty)
filterQuestionsByDifficulty(difficulty) {
if (typeof difficulty === 'number' && difficulty >= 1 && difficulty <= 3) {
this.questions = this.questions.filter(question => question.difficulty === difficulty);
}
}
// averageDifficulty()
averageDifficulty() {
if (this.questions.length === 0) return 0;
const total = this.questions.reduce((acc, q) => {
return acc + q.difficulty;
}, 0);
return total / this.questions.length;
}
}