-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclasses.js
More file actions
51 lines (44 loc) · 1.18 KB
/
classes.js
File metadata and controls
51 lines (44 loc) · 1.18 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
// classes.js
// Saira
// Creates a new object witht the clss question
// A template to make question object
// This class is not being used at all
export class Question {
constructor({ question, correct, incorrect }) {
this.question = question;
this.correct = correct;
this.incorrect = incorrect.slice(); // copy array
}
// return shuffled answers array
getShuffledAnswers() {
const arr = [...this.incorrect, this.correct];
return shuffleArray(arr);
}
isCorrect(answer) {
return answer === this.correct;
}
}
// TimedQuestion extends Question and adds a per-question timer
export class TimedQuestion extends Question {
constructor(data, timeLimit = 20) {
super(data);
this.timeLimit = timeLimit;
this.timeRemaining = timeLimit;
}
tick() {
this.timeRemaining = Math.max(0, this.timeRemaining - 1);
return this.timeRemaining;
}
resetTimer() {
this.timeRemaining = this.timeLimit;
}
}
// small shuffle helper
function shuffleArray(a) {
const arr = a.slice();
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arr[i], arr[j]] = [arr[j], arr[i]];
}
return arr;
}