-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (65 loc) · 2.33 KB
/
script.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
const oXHR = new XMLHttpRequest();
const question = document.querySelector(".question");
const option1 = document.querySelector("#option1");
const option2 = document.querySelector("#option2");
const option3 = document.querySelector("#option3");
const option4 = document.querySelector("#option4");
const submit = document.querySelector("#submit");
const showScore = document.querySelector("#showScore");
const answers = document.querySelectorAll(".answer");
let questionCount = 0;
let score = 0;
function reportStatus() {
if (oXHR.readyState == 4 && this.status == 200) {
getQuestion = JSON.parse(this.responseText);
//getting data when ever page get loaded
const loadQuestion = () => {
const questionList = getQuestion[questionCount];
question.innerHTML = questionList.question;
option1.innerHTML = questionList.a;
option2.innerHTML = questionList.b;
option3.innerHTML = questionList.c;
option4.innerHTML = questionList.d;
};
loadQuestion();
//=====================================================================================
// Checking answers
// ===================================================================================
const getCheckAnswer = () => {
let answer;
answers.forEach((currAnsElem) => {
if (currAnsElem.checked) {
answer = currAnsElem.id;
}
});
return answer;
};
const disSelectAll = () => {
answers.forEach((currAnsElem) => (currAnsElem.checked = false));
};
submit.addEventListener("click", () => {
const checkAnswer = getCheckAnswer();
// console.log(checkAnswer);
if (checkAnswer === getQuestion[questionCount].ans) {
score++;
}
//updating questions when user submit
questionCount++;
disSelectAll();
if (questionCount < getQuestion.length) {
loadQuestion();
} else {
showScore.innerHTML = `
<h3>Your scored: ${score}/${getQuestion.length} ✌</h3>
<button class = "btn" onclick ="location.reload()">REST AGAIN</button>
`;
showScore.classList.remove("scoreArea");
submit.style.display = "none";
}
});
}
}
//initialize request
oXHR.onreadystatechange = reportStatus;
oXHR.open("GET", "quizcpp.json", true);
oXHR.send();