-
Notifications
You must be signed in to change notification settings - Fork 483
Expand file tree
/
Copy pathquiz.js
More file actions
53 lines (47 loc) · 1.19 KB
/
quiz.js
File metadata and controls
53 lines (47 loc) · 1.19 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
class Quiz {
constructor(questions, timeLimit, timeRemaining) {
this.questions = questions;
this.timeLimit = timeLimit;
this.timeRemaining = timeRemaining;
this.correctAnswers = 0;
this.currentQuestionIndex = 0;
}
getQuestion() {
return this.questions[this.currentQuestionIndex];
}
moveToNextQuestion() {
return this.currentQuestionIndex++;
}
shuffleQuestions() {
this.questions.sort(() => Math.random() - 0.5);
}
checkAnswer(answer) {
if (answer === this.getQuestion().answer) {
this.correctAnswers++;
}
}
hasEnded() {
if (this.currentQuestionIndex < this.questions.length) {
return false;
} else {
return true;
}
}
filterQuestionsByDifficulty(difficulty) {
if (typeof difficulty !== "number" || difficulty < 1 || difficulty > 3)
return;
this.questions = this.questions.filter((question) => {
if (difficulty === question.difficulty) {
return question;
}
});
}
averageDifficulty() {
const total = this.questions.reduce(
(total, question) => total + question.difficulty,
0
);
const average = total / this.questions.length;
return average;
}
}