-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
129 lines (114 loc) · 3.96 KB
/
script.js
File metadata and controls
129 lines (114 loc) · 3.96 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
// DOM Elements
const startScreen = document.getElementById("start-screen");
const quizScreen = document.getElementById("quiz-screen");
const resultScreen = document.getElementById("result-screen");
const startButton = document.getElementById("start-btn");
const questionText = document.getElementById("question-text");
const answersContainer = document.getElementById("answers-container");
const currentQuestionSpan = document.getElementById("current-question");
const totalQuestionsSpan = document.getElementById("total-questions");
const scoreSpan = document.getElementById("score");
const finalScoreSpan = document.getElementById("final-score");
const maxScoreSpan = document.getElementById("max-score");
const resultMessage = document.getElementById("result-message");
const restartButton = document.getElementById("restart-btn");
const progressBar = document.getElementById("progress");
// setting up the quiz using api
const quizUrl =
"https://opentdb.com/api.php?amount=4&category=18&difficulty=easy&type=multiple";
async function apiQues() {
try {
let response = await axios.get(quizUrl);
let data = await response;
return data.data.results;
} catch (er) {
console.log(er);
}
}
let quizQuestions = {};
let currentQuestion = "";
// storing the questions before for a smooth experience
apiQues().then((data) => {
quizQuestions = data;
totalQuestionsSpan.textContent = quizQuestions.length;
maxScoreSpan.textContent = quizQuestions.length;
});
// Question state
let currentQuestionIndex = 0;
let score = 0;
let answers = "";
// eventListener
startButton.addEventListener("click", startQuiz);
restartButton.addEventListener("click", restartQuiz);
function startQuiz() {
console.log("quiz started");
// reset vars
currentQuestionIndex = 0;
score = 0;
scoreSpan.textContent = score;
progressBar.style.width = 0 + "%";
startScreen.classList.remove("active");
quizScreen.classList.add("active");
showQuestion();
showAnswer();
}
async function showQuestion() {
const progressPercent = (currentQuestionIndex / quizQuestions.length) * 100;
progressBar.style.width = progressPercent + "%";
currentQuestion = quizQuestions[currentQuestionIndex].question;
questionText.textContent = currentQuestion;
currentQuestionSpan.textContent = currentQuestionIndex + 1;
//console.log(currentQuestion);
}
async function showAnswer() {
let currentQ = quizQuestions[currentQuestionIndex]; // pick current question
answersContainer.innerHTML = "";
let allAnswers = [...currentQ.incorrect_answers, currentQ.correct_answer];
allAnswers.sort(() => Math.random() - 0.5); // shuffle answers
allAnswers.forEach((answer) => {
let btn = document.createElement("button");
btn.textContent = answer;
// add click event on the button
btn.addEventListener("click", function () {
if (answer === currentQ.correct_answer) {
btn.classList.add("correct");
score++;
scoreSpan.textContent = score;
} else {
btn.classList.add("incorrect"); // red
}
// optionally disable all buttons after clicking one answer
Array.from(answersContainer.children).forEach(
(btn) => (btn.disabled = true)
);
nextQues();
});
answersContainer.appendChild(btn);
});
}
function nextQues() {
setTimeout(() => {
currentQuestionIndex++;
if (currentQuestionIndex < quizQuestions.length) {
showQuestion();
showAnswer();
} else {
currentQuestionIndex++;
showResults();
}
}, 400);
}
function showResults() {
resultScreen.classList.add("active");
quizScreen.classList.remove("active");
finalScoreSpan.textContent = score;
}
restartButton.addEventListener("click", () => {
resultScreen.classList.remove("active");
startScreen.classList.add("active");
restartQuiz();
});
function restartQuiz() {
location.reload();
console.log("quiz restarted");
}