-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (100 loc) · 3.02 KB
/
index.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
const quizData = [
{
question: 'What does HTML stand for?',
a: 'Hyper Text Preprocessor',
b: 'Hyper Text Markup Language',
c: 'Hyper Text Multiple Language',
d: 'Hyper Tool Multi Language',
answer: 'b',
},
{
question: 'What does CSS stand for?',
a: 'Common Style Sheet',
b: 'Colorful Style Sheet',
c: 'Computer Style Sheet',
d: 'Cascading Style Sheet',
answer: 'd',
},
{
question: 'What does PHP stand for?',
a: 'Hypertext Preprocessor',
b: 'Hypertext Programming',
c: 'Hypertext Preprogramming',
d: 'Hometext Preprocessor',
answer: 'a',
},
{
question: 'What does SQL stand for?',
a: 'Stylish Question Language',
b: 'Stylesheet Query Language',
c: 'Statement Question Language',
d: 'Structured Query Language',
answer: 'd',
},
{
question: 'What does XML stand for?',
a: 'eXtensible Markup Language',
b: 'eXecutable Multiple Language',
c: 'eXTra Multi-Program Language',
d: 'eXamine Multiple Language',
answer: 'a',
},
];
const quiz = document.querySelector(".quiz-body");
const answerEl = document.querySelectorAll(".answer");
const questionEl = document.getElementById("question");
const footerEl = document.querySelector(".quiz-footer");
const quizDetailEl = document.querySelector(".quiz-details");
const liEl = document.querySelector("ul li");
const a_txt = document.getElementById("a_text");
const b_txt = document.getElementById("b_text");
const c_txt = document.getElementById("c_text");
const d_txt = document.getElementById("d_text");
const btnSubmit = document.getElementById("btn");
let currentQuiz = 0;
let score = 0;
loadQuiz();
function loadQuiz() {
deselectAnswers();
const currentQuizData = quizData[currentQuiz];
questionEl.innerText = currentQuizData.question;
a_txt.innerText = currentQuizData.a;
b_txt.innerText = currentQuizData.b;
c_txt.innerText = currentQuizData.c;
d_txt.innerText = currentQuizData.d;
quizDetailEl.innerHTML = `<p>${currentQuiz + 1} of ${quizData.length}</p>`;
}
function deselectAnswers() {
answerEl.forEach((answerEl) => {
answerEl.checked = false;
});
}
function getSelected() {
let answer;
answerEl.forEach((answerEls) => {
if (answerEls.checked) {
answer = answerEls.id;
}
});
return answer;
}
btnSubmit.addEventListener("click", function () {
const answers = getSelected();
if (answers) {
if (answers === quizData[currentQuiz].answer) {
score++;
}
nextQuestion();
}
});
function nextQuestion() {
currentQuiz++;
if (currentQuiz < quizData.length) {
loadQuiz();
} else {
quiz.innerHTML = `<h2>You answered ${score}/${quizData.length} question correctly</h2>
<button type="button" onclick="location.reload()">Reload</button>
`;
footerEl.style.display = "none";
}
}