-
Notifications
You must be signed in to change notification settings - Fork 13
/
quiz.js
287 lines (255 loc) · 7.8 KB
/
quiz.js
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
const questions = [{
question: "Nazwa zmiennej nie może zaczynać się od:",
answers: {
a: "dolara",
b: "cyfry",
c: "dużej litery"
},
correctAnswer: "b"
},
{
question: "function square(number) {\r\n return number * number; \r\n } \r\nto przykład:",
answers: {
a: "deklaracji funkcji",
b: "wywołania funkcji",
c: "domknięcia funkcji"
},
correctAnswer: "a"
},
{
question: "Który operator porównuje dwa operandy z uwzględnieniem ich typu?",
answers: {
a: "==",
b: "!=",
c: "==="
},
correctAnswer: "c"
},
{
question: "Inna nazwa języka JavaScript to:",
answers: {
a: "ActionScript",
b: "Java",
c: "ECMAScript"
},
correctAnswer: "c"
},
{
question: "Która z poniższych metod wywołuje funkcję dla każdego elementu tablicy?",
answers: {
a: "forEach()",
b: "each()",
c: "withEach()"
},
correctAnswer: "a"
},
{
question: "W jaki sposób komentujemy linię w JavaScript:",
answers: {
a: "<--To jest komentarz -->",
b: "// To jest komentarz",
c: "** To jest komentarz **"
},
correctAnswer: "b"
},
{
question: "Przykład prawidłowej składni instrukcji if to:",
answers: {
a: "if i==5 then",
b: "if i=5",
c: "if(i==5)"
},
correctAnswer: "c"
},
{
question: "Które ze zdarzeń ma miejsce podczas kliknięcia elementu HTML?:",
answers: {
a: "onclick",
b: "ondrop",
c: "onmouseclick"
},
correctAnswer: "a"
},
{
question: "Który ze sposobów przechowywania danych zapisuje je na komputerze klienta bez ograniczeń czasowych?",
answers: {
a: "sessionStorage",
b: "cookie",
c: "localStorage"
},
correctAnswer: "c"
},
{
question: "Jak wybrać z dokumentu pierwszy element o klasie 'top':",
answers: {
a: "document.querySelector('.top');",
b: "document.querySelectorAll('.top)",
c: "document.querySelector('top')"
},
correctAnswer: "a"
}
];
//INFORMATION PANEL
function next(i) {
const step = document.getElementById(`step${i}`);
const isActive = document.querySelector(".is-active");
isActive && isActive.classList.remove("is-active");
isActive && isActive.classList.add("is-complete");
step && step.classList.add("is-active");
}
//END INFORMATION PANEL
// show next question
const nextButton = document.getElementById('next-btn');
nextButton.addEventListener("click", showNextQuestion);
let currentQuestion = 0;
let correctAnswers = 0;
// show result
const resultButton = document.getElementById('result-btn');
resultButton.addEventListener("click", function () {
showResult('Koniec Quizu!', true)
}, false);
resultButton.style.display = 'none';
// start quiz
const lastButton = document.getElementById('last-btn');
lastButton.addEventListener('click', startQuiz);
lastButton.style.display = 'none';
// show correct answer
const showAnswerButton = document.getElementById('answer-btn');
showAnswerButton.addEventListener('click', showCorrectAnswer);
// timer
let myTimer,
timerDisplay = document.querySelector('#time');
function startTimer() {
let minutes,
seconds,
duration = 300;
function timer() {
duration--;
minutes = (duration / 60) | 0;
seconds = (duration % 60) | 0;
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
timerDisplay.textContent = `Zostało ${minutes} : ${seconds} żeby zakończyć Quiz!`;
if (duration <= 0) {
clearInterval(myTimer);
showResult('Skończył się czas. Koniec Quizu!', false);
}
}
timer();
myTimer = clearInterval(myTimer);
myTimer = setInterval(timer, 1000);
}
function stopTimer() {
clearInterval(myTimer);
}
// show question
function showQuestion(i) {
let paragraph = document.querySelector('main p');
paragraph.textContent = questions[i].question;
showAnswers(i);
next(i);
}
//show answers
function showAnswers(questionIndex) {
const q = questions[questionIndex];
const option1 = document.getElementById('answer1_label');
const option2 = document.getElementById('answer2_label');
const option3 = document.getElementById('answer3_label');
option1.innerHTML = `<input type = "radio" id = "answer1" name = "answer" value = "a" >
</input> <span class="label-text">${q.answers.a}</span>
<span class="checkmark"></span>`;
option2.innerHTML = `<input type = "radio" id = "answer2" name = "answer" value = "b" > </input>
<span class="label-text">${q.answers.b} </span>
<span class="checkmark"></span>`;
option3.innerHTML = `<input type = "radio" id = "answer3" name = "answer" value = "c" > </input>
<span class="label-text">${q.answers.c} </span><span class="checkmark"></span>`;
}
function showNextQuestion() {
verify();
currentQuestion += 1;
showQuestion(currentQuestion);
showAnswerButton.style.display = 'block';
if (currentQuestion === questions.length - 1) {
nextButton.style.display = 'none';
resultButton.style.display = 'block';
}
}
// show answer for current question
function showCorrectAnswer() {
let correctAnswerLetter = questions[currentQuestion].correctAnswer;
let radioButtons = document.getElementsByName("answer");
[...radioButtons].map(item => {
if (item.value === correctAnswerLetter) {
item.checked = true;
correctAnswers--;
}
});
showAnswerButton.style.display = 'none';
}
// verify currentQuestion. if it's correct increase correctAnswers counter
function verify() {
let currentAnswer = document.querySelector('input[name=answer]:checked').value;
if (currentAnswer && questions[currentQuestion].correctAnswer === currentAnswer) {
correctAnswers++;
}
}
function showResult(titleText, callVerify) {
if (callVerify) {
verify();
stopTimer();
}
renderResultText(titleText);
manageButtonsDisplay();
let step = document.getElementById(`step${currentQuestion}`);
step.classList.add("is-complete");
step.classList.remove("is-active");
}
function startQuiz() {
correctAnswers = 0;
timerDisplay.style.display = 'block';
showAnswerButton.style.display = 'block';
const mainElement = document.querySelector('main');
mainElement.innerHTML = `
<p>treść pytania</p>
<radiogroup class="answers">
<label id="answer1_label"><input type="radio" id="answer1" name="answer" value="a"/>odpowiedź pierwsza</label>
<label id="answer2_label"><input type="radio" id="answer2" name="answer" value="b"/>odpowiedź druga</label>
<label id="answer3_label"><input type="radio" id="answer3" name="answer" value="c"/>odpowiedź trzecia</label>
</radiogroup>
<p></p>
`;
renderProgressSteps();
currentQuestion = 0;
lastButton.style.display = 'none';
nextButton.style.display = 'block';
let completeSteps = document.getElementsByClassName('is-complete');
completeSteps && Array.from(completeSteps).forEach(el => el.classList.remove('is-complete'));
showQuestion(currentQuestion);
startTimer();
}
function renderResultText(titleText) {
let contentText = `Odpowiedziałaś dobrze na ${correctAnswers} z ${questions.length} pytań.`;
const mainElement = document.querySelector('main');
mainElement.innerHTML = `
<p>${titleText}</p>
<p>${contentText}</p>
`;
}
function manageButtonsDisplay() {
resultButton.style.display = 'none';
lastButton.style.display = 'block';
showAnswerButton.style.display = 'none';
timerDisplay.style.display = 'none';
nextButton.style.display = 'none';
}
function renderProgressSteps() {
const progressStep = document.getElementsByClassName('progress-step');
if (progressStep.length >= questions.length) {
Array.from(progressStep).forEach(el => el.remove());
}
let progressContainer = document.getElementById('progress');
for (let i = 0; i < questions.length; i++) {
progressContainer.innerHTML += `<div id='step${i}' class="progress-step"></div>`;
}
}
startQuiz();