-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (133 loc) · 4.19 KB
/
script.js
File metadata and controls
156 lines (133 loc) · 4.19 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
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
const quizes = [
{
question: "In which Italian city can you find the Colosseum?",
correct_index: 0,
answers: ["Venice", "Rome", "Naples", "Milan"],
},
{
question: "What is the largest canyon in the world?",
correct_index: 2,
answers: ["Venice", "Rome", "Naples", "Milan"],
},
{
question: "In the TV show New Girl, which actress plays Jessica Day?",
correct_index: 3,
answers: ["Venice", "Rome", "Naples", "Milan"],
},
];
let curr_quiz = 0;
let correct_ans = 0;
let clearFunctions = [];
const quizTitle = document.getElementById("quiz-title");
const nextBtn = document.getElementById("btn-next");
const scorePannel = document.getElementById("score-panel");
const quizPannel = document.getElementById("quiz-panel");
const answerPannel = document.getElementById("anwser-panel");
const answerBtns = document.getElementsByClassName("answer-btn");
const score = document.getElementById("score-precentage");
const scoreDiv = document.getElementById("score-devide");
const startBtn = document.getElementById("start");
const startPannel = document.getElementById("start-panel");
const restartBtn = document.getElementById("restart");
const quizAppTitle = document.getElementById("quiz-app--title");
const quizNo = document.getElementById("quiz-no");
const feedback = document.getElementById("quiz-feedback");
const feedbackSection = document.getElementById("feedback-section");
//Event Listners
nextBtn.addEventListener("click", nextQuiz);
startBtn.addEventListener("click", startQuiz);
restartBtn.addEventListener("click", startQuiz);
// Add Event Listner for all answer buttons
for (let btn of answerBtns) {
btn.addEventListener("click", submitAnswer);
}
function disableAllButtons() {
for (let btn of answerBtns) {
btn.setAttribute("disabled", "true");
}
clearFunctions.push(() => {
for (let btn of answerBtns) {
btn.removeAttribute("disabled");
}
});
}
function startQuiz() {
quizPannel.classList.remove("hidden");
startPannel.classList.add("hidden");
quizAppTitle.classList.add("hidden");
curr_quiz = 0;
correct_ans = 0;
nextQuiz();
}
function nextQuiz() {
clearPrev();
if (quizes.length <= curr_quiz) {
nextBtn.classList.add("hidden");
showScore();
curr_quiz = 0;
return false;
}
nextBtn.classList.add("hidden");
showQuestion();
curr_quiz++;
quizNo.innerText = `Q.0${curr_quiz}`;
}
function showScore() {
quizPannel.classList.add("hidden");
scorePannel.classList.remove("hidden");
feedbackSection.classList.add("hidden");
confetti({
particleCount: 100,
spread: 70,
origin: { y: 0.4 },
});
score.innerText = ` ${Math.round((correct_ans / quizes.length) * 100)}%`;
scoreDiv.innerText = `${correct_ans}/${quizes.length} Score`;
clearFunctions.push(() => {
quizPannel.classList.remove("hidden");
scorePannel.classList.add("hidden");
});
}
function showQuestion() {
const question = quizes[curr_quiz];
quizTitle.innerText = question.question;
for (let i = 0; i < 4; i++) {
answerBtns[i].innerText = question.answers[i];
answerBtns[i].dataset.index = i;
}
}
function clearPrev() {
for (let func of clearFunctions) {
func();
}
clearFunctions = [];
}
function submitAnswer(event) {
let answer = parseInt(event.target.dataset.index);
let correctAnswer = parseInt(quizes[curr_quiz - 1].correct_index);
disableAllButtons();
nextBtn.classList.remove("hidden");
feedbackSection.classList.remove("hidden");
if (answer === correctAnswer) {
answerBtns[correctAnswer].classList.add("correct");
feedback.innerText = "Correct Answer";
feedback.classList.add("correct-ans");
correct_ans++;
clearFunctions.push(() => {
answerBtns[correctAnswer].classList.remove("correct");
feedback.innerText = "";
feedback.classList.remove("correct-ans");
});
return;
}
answerBtns[correctAnswer].classList.add("correct");
answerBtns[answer].classList.add("wrong");
feedback.innerText = "Wrong Answer";
feedback.classList.add("wrong-ans");
clearFunctions.push(() => {
answerBtns[correctAnswer].classList.remove("correct");
answerBtns[answer].classList.remove("wrong");
feedback.innerText = "";
feedback.classList.remove("Wrong-ans");
});
}