-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquiz.js
129 lines (112 loc) · 3.39 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
var question = document.getElementById("questions");
var option = document.getElementsByName("option");
var next = document.getElementById("next");
var previous = document.getElementById("prev");
var result = document.getElementById("result");
var button = document.querySelector(".btn1");
var A = document.getElementById("option1");
var B = document.getElementById("option2");
var C = document.getElementById("option3");
var D = document.getElementById("option4");
var questioId = document.getElementById("question-number");
const arr = [];
fetch("https://task-3-api.herokuapp.com/questions")
.then((response) => response.json())
.then(
(
data // console.log(data));
) => {
for (j = 0; j < data.length; j++) {
var obj = {};
obj["questionId"] = data[j].questionId;
obj["question"] = data[j].question;
obj["optionA"] = data[j].optionA;
obj["optionB"] = data[j].optionB;
obj["optionC"] = data[j].optionC;
obj["optionD"] = data[j].optionD;
obj["correctOption"] = data[j].correctOption;
arr.push(obj);
// console.log(obj);
}
}
);
var score = 0;
var i = 0;
function questions() {
questioId.innerText = arr[i].questionId;
question.innerText = arr[i].question;
A.innerText = arr[i].optionA;
B.innerText = arr[i].optionB;
C.innerText = arr[i].optionC;
D.innerText = arr[i].optionD;
}
option.forEach((val) => {
val.addEventListener("click", () => {
if (val.checked) {
if (val.value === arr[i].correctOption) {
score++;
option.forEach((val) => {
val.classList.add("response");
});
} else {
score--;
option.forEach((val) => {
val.classList.add("response");
});
}
}
});
});
const ans = document.querySelectorAll(".ans");
// Previous button
previous.addEventListener("click", () => {
if (i != 0) {
i -= 1;
}
questions();
value.classList.remove("response");
});
// next button
next.addEventListener("click", () => {
i++;
if (i < arr.length) {
questions();
option.forEach((value) => {
option.forEach((val) => {
val.checked = false;
});
value.classList.remove("response");
});
} else {
resultPage();
}
if (i == 9) {
next.innerText = "Submit";
} else {
next.innerText = "Next";
}
});
function resultPage() {
const container = document.querySelector(".quiz-container");
container.innerHTML = "";
const result = document.getElementsByTagName("body");
result[0].innerHTML = `<div class="res"><div class="scor"><h1>You Scored: <span>${score}</span> out of 10</h1></div><div class="res-end"><form method="post"><button type="submit" href="quiz.html">Play again!</button></form></div></div>`;
}
var totaltime = 60 * 10 - 1,
display = document.getElementById("clock");
startTimer(totaltime, display);
function startTimer(duration, display) {
var timer = duration,
minutes,
seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10);
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.textContent = minutes + ":" + seconds + "minute left";
if (--timer < 0) {
timer = duration;
}
}, 1000);
}