forked from Amiya-Maity/Quizzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (62 loc) · 2.32 KB
/
script.js
File metadata and controls
72 lines (62 loc) · 2.32 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
document.getElementById("categories").addEventListener("click", function () {
let categoryList = document.getElementById("category-list");
categoryList.classList.toggle("hidden");
});
var submitButtonClickCount = 0;
document.getElementById('quiz-form').addEventListener('submit', function (event) {
event.preventDefault();
submitButtonClickCount++;
submitAnswers();
});
if (submitButtonClickCount === 0) {
let myVar = setInterval(myTimer, 1000);
let time = parseInt(document.getElementById("timer").getAttribute("data-time"));
function myTimer() {
time--;
let timer = document.getElementById("timer");
timer.innerHTML = formatTime(time);
if (time === 0) {
clearInterval(myVar);
alert("Time's Up!!");
document.getElementById("submit").click();
}
}
function formatTime(seconds) {
let minutes = Math.floor((seconds % 3600) / 60);
let remainingSeconds = seconds % 60;
return pad(minutes) + ":" + pad(remainingSeconds);
}
function pad(number) {
return number < 10 ? "0" + number : number;
}
}
function restartPage() {
location.reload();
}
function submitAnswers() {
var form = document.getElementById('quiz-form');
var formData = new FormData(form);
var xhr = new XMLHttpRequest();
xhr.open("POST", "index.php", true);
xhr.onreadystatechange = function () {
if (xhr.readyState === 4 && xhr.status === 200) {
var resultContainer = document.getElementById("result");
var results = JSON.parse(xhr.responseText).results;
for (var questionId in results) {
var optionElements = document.querySelectorAll('input[name="answers[' + questionId + ']"]');
optionElements.forEach(function (optionElement) {
optionElement.parentNode.classList.remove('correct', 'incorrect');
if (results[questionId] === 'Correct!') {
optionElement.parentNode.classList.add('correct');
} else {
optionElement.parentNode.classList.add('incorrect');
}
});
}
}
};
xhr.send(formData);
}
function selectRadioButton(divId) {
document.getElementById(divId).checked = true;
}