-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
164 lines (141 loc) · 4.2 KB
/
app.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
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
157
158
159
160
161
162
163
164
function randomize(values) {
let index = values.length,
randomIndex;
// While there remain elements to shuffle.
while (index != 0) {
// Pick a remaining element.
randomIndex = Math.floor(Math.random() * index);
index--;
// And swap it with the current element.
[values[index], values[randomIndex]] = [values[randomIndex], values[index]];
}
return values;
}
var option;
var incorrectAns;
var correctAnswer;
var apiIndexQues = 0;
var totalQuestions;
var all_json_data;
var userSelection = [];
var showColor = false;
function changeColor() {
if (showColor) {
checkAnswer();
}
}
function calculateScore() {
showColor = true;
var score = 0;
for (let i = 0; i < all_json_data.length; i++) {
if (all_json_data[i].correct_answer === userSelection[i]) {
score++;
}
}
var userScore = document.getElementById("score");
userScore.innerHTML = `Score ${score} out of 10`;
}
function calculateScoreButtonVisibility(apiIndexQues) {
const checkAnswerBtn = document.getElementById("btn1");
checkAnswerBtn.addEventListener("click", calculateScore);
if (apiIndexQues === 9) {
checkAnswerBtn.style.visibility = "visible";
} else {
checkAnswerBtn.style.visibility = "hidden";
}
}
function displayQuestions(apiIndexQues) {
previousButtonVisibility(apiIndexQues);
nextButtonVisibility(apiIndexQues);
calculateScoreButtonVisibility(apiIndexQues);
let displayQuesElement = document.getElementById("displayQues");
option = document.querySelectorAll(".option");
incorrectAns = [...all_json_data[apiIndexQues].incorrect_answers];
correctAnswer = all_json_data[apiIndexQues].correct_answer;
incorrectAns.push(correctAnswer);
randomize(incorrectAns);
displayQuesElement.innerHTML = all_json_data[apiIndexQues].question;
for (let i = 0; i < incorrectAns.length; i++) {
option[i].nextElementSibling.innerText = incorrectAns[i];
}
changeColor();
}
//next Button
function nextButton() {
apiIndexQues++;
displayQuestions(apiIndexQues);
checkedAnswers(apiIndexQues);
}
function nextButtonVisibility(apiIndexQues) {
var next = document.getElementById("btn");
next.addEventListener("click", nextButton);
if (apiIndexQues === 9) {
next.style.visibility = "hidden";
} else {
next.style.visibility = "visible";
}
}
function checkedAnswers(index) {
var thisisUserSelectedValue = userSelection[index];
if (thisisUserSelectedValue === undefined) {
for (let i = 0; i < incorrectAns.length; i++) {
option[i].checked = false;
}
return;
}
for (let i = 0; i < incorrectAns.length; i++) {
if (incorrectAns[i] == thisisUserSelectedValue) {
option[i].checked = true;
}
}
}
// previous Button
function previousButton() {
console.log(apiIndexQues);
apiIndexQues--;
displayQuestions(apiIndexQues);
checkedAnswers(apiIndexQues);
}
function previousButtonVisibility(apiIndexQues) {
var previous = document.getElementById("btn2");
previous.addEventListener("click", previousButton);
if (apiIndexQues === 0) {
previous.style.visibility = "hidden";
} else {
previous.style.visibility = "visible";
}
}
// Radio Button
function userSelectedAnswer() {
const radioButton = document.getElementsByName("answer");
radioButton.forEach((a, b) => {
if (a.checked) {
userSelection[apiIndexQues] = incorrectAns[b];
}
});
}
const choiceText = document.getElementsByName("answer");
for (var i = 0; i < choiceText.length; i++) {
choiceText[i].addEventListener("click", userSelectedAnswer);
}
function checkAnswer() {
$("label").removeClass("incorrectChoice");
$("label").removeClass("correctChoice");
console.log(correctAnswer);
const radioButton = document.getElementsByName("answer");
radioButton.forEach((a, b) => {
console.log(a);
if (incorrectAns[b] === correctAnswer) {
$("label[for=" + a.id + "]").addClass("correctChoice");
} else {
$("label[for=" + a.id + "]").addClass("incorrectChoice");
}
});
}
let apiUrl = `https://opentdb.com/api.php?amount=10&category=9&difficulty=medium&type=multiple`;
axios.get(apiUrl).then(saveResponse);
function saveResponse(response) {
all_json_data = response.data.results;
console.log(all_json_data);
displayQuestions(apiIndexQues);
}