-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz.js
More file actions
138 lines (107 loc) · 3.79 KB
/
quiz.js
File metadata and controls
138 lines (107 loc) · 3.79 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let next = document.querySelector('.next');
let form = document.querySelector('form');
let prev = document.querySelector('.prev');
let quizElm = document.querySelector(".quiz");
let totalQuestions = document.querySelector('header p');
let showResult = document.querySelector('.show_result');
class Question {
constructor(title, options, correctAnswerIndex) {
this.title = title;
this.options = options;
this.correctAnswerIndex = correctAnswerIndex;
}
isCorrect(answer) {
return this.options[this.correctAnswerIndex] === answer;
}
getCorrectAnswer() {
return this.options[this.correctAnswerIndex]
}
}
class Quiz {
constructor(questions = [], score = 0) {
this.questions = questions;
this.score = score;
this.activeIndex = 0;
}
nextQuestion() {
this.activeIndex = this.activeIndex + 1;
this.createUi();
}
prevQuestion() {
this.activeIndex = this.activeIndex - 1;
this.createUi();
}
incrementScore() {
this.score = this.score + 1;
}
addQuestion(title, options, answerIndex) {
let question = new Question(title, options, answerIndex);
this.questions.push(question);
}
handleButton() {
if (this.activeIndex === 0) {
prev.style.visibility = "hidden";
} else if (
this.activeIndex === this.questions.length - 1) {
next.style.visibility = "hidden";
showResult.style.visibility = "visible";
} else {
prev.style.visibility = "visible"
next.style.visibility = "visible"
showResult.style.visibility = "hidden";
}
}
createUi() {
quizElm.innerHTML = "";
let activeQuestion = this.questions[this.activeIndex];
let form = document.createElement("form");
let fieldset = document.createElement("fieldset");
let legend = document.createElement("legend");
legend.innerText = activeQuestion.title;
let optionsElm = document.createElement("div");
optionsElm.classList.add("options");
let buttonWrapper = document.createElement("div");
let button = document.createElement("button");
button.type = "submit"
button.innerText = "Submit";
buttonWrapper.append(button);
activeQuestion.options.forEach((option, index) => {
let input = document.createElement("input");
let div = document.createElement("div");
input.id = `option-${index}`;
input.type = "radio";
input.name = "options";
input.value = option;
let label = document.createElement("label");
label.for = `option-${index}`;
label.innerText = option;
form.addEventListener("submit", (event) => {
event.preventDefault();
if (input.checked) {
if (activeQuestion.isCorrect(input.value)) {
this.incrementScore();
}
}
});
div.append(input, label);
optionsElm.append(div);
});
this.handleButton();
totalQuestions.innerText = `Total questions : ${this.questions.length}`;
fieldset.append(legend, optionsElm, buttonWrapper);
form.append(fieldset);
quizElm.append(form);
}
}
let quiz = new Quiz();
quizCollection.forEach((question) => {
quiz.addQuestion(question.title,
question.options,
question.answerIndex);
});
quiz.createUi();
next.addEventListener("click", quiz.nextQuestion.bind(quiz));
prev.addEventListener("click", quiz.prevQuestion.bind(quiz));
showResult.addEventListener("click", () => {
alert(`Your score is ${quiz.score}`);
});