-
Notifications
You must be signed in to change notification settings - Fork 2
/
quiz.js
153 lines (138 loc) · 6.07 KB
/
quiz.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
//import for using local storage
import { local } from 'wix-storage';
$w.onReady(function () {
//defining some variables
let score = 0; //contain the score
let data; //will contain response from api
let start = 6; // starting no. of the button with options on them
//getting the values for theme and level selected on themes.js page
let theme = local.getItem("theme");
let level = local.getItem("level");
//creating an ajax request
const xhr = new XMLHttpRequest();
xhr.open(
"GET",
`https://opentdb.com/api.php?amount=5&category=${theme}&difficulty=${level}&type=multiple`,
true
);
//dealing with the response from api
xhr.onload = function () {
data = JSON.parse(this.responseText) //data is now a object
data = (data["results"]) //"results" key in data contains questions and answers
//traversing over the data object which is somewhat like this
/*
{
"response_code": 0,
"results": [
{
"category": "Science & Nature",
"type": "multiple",
"difficulty": "easy",
"question": "Which is the longest bone in the human body? ",
"correct_answer": "Femur",
"incorrect_answers": [
"Scapula",
"Fibula",
"Ulna"
]
},
{
"category": "Science & Nature",
"type": "multiple",
"difficulty": "easy",
"question": "What is the first element on the periodic table?",
"correct_answer": "Hydrogen",
"incorrect_answers": [
"Helium",
"Oxygen",
"Lithium"
]
},......
*/
for (let index = 0; index < data.length; index++) {
let question = (data[index]["question"]); //getting the question
//modifying question: replacing html codes with their respective special characters
question = question.replaceAll('&', '&');
question = question.replaceAll('>', '>');
question = question.replaceAll('<', '<');
question = question.replaceAll('"', '"');
question = question.replaceAll('#', '#');
question = question.replaceAll('&', '&');
question = question.replaceAll(''', "'");
question = question.replaceAll('(', '(');
question = question.replaceAll(')', ')');
question = question.replaceAll(',', ',');
question = question.replaceAll('-', '-');
question = question.replaceAll('/', ')');
question = question.replaceAll(')', '/');
question = question.replaceAll(':', ':');
question = question.replaceAll(';', ';');
question = question.replaceAll('<', '<');
question = question.replaceAll('>', '>');
question = question.replaceAll('=', '=');
question = question.replaceAll('?', '?');
question = question.replaceAll('@', '@');
question = question.replaceAll('_', '_');
question = question.replaceAll('°', '°');
question = question.replaceAll(''', "'");
let options = data[index]["incorrect_answers"]; //first storing the incorrect ans into options array
options.push(data[index]["correct_answer"]) //storing the correct answer from data object
//randomly changing the position of correct ans
let randomPosition = Math.floor(Math.random() * (3 - 0 + 1)) + 0 //computing a random position for correct ans to be swapped with the array element at this random position
//swapping
let temp = options[3];
options[3] = options[randomPosition];
options[randomPosition] = temp;
// console.log("Question\n", question, "\nOPtions\n", options)
//filling the text boxes on webpage with questions
$w(`#textBox${index+1}`).value = question;
//filling the buttons on webpage with options
for (let i = 0; i <= 3; i++) {
options[i] = options[i].replaceAll(''', "'");
options[i] = options[i].replaceAll('°', '°');
options[i] = options[i].replaceAll('"', '"');
$w(`#button${start}`).label = options[i];
start++;
}
}
}
xhr.send(); //sending the request
//after user clicks on any option
//storing the users answers
//btn 6,7,8,9-> question 1
//btn 10,11,12,13 ->q 2
//btn 14,15,16,17-> q3
//btn 18,19,20,21->q4
//btn 22 23 24 25->q5
for (let index = 6; index <= 25; index++) {
$w(`#button${index}`).onClick(function () {
if (index >= 22) { //it's q5
let userAns = $w(`#button${index}`).label;
if (userAns == data[4]["correct_answer"]) { //if user clicked ans is same as correct ans then increase the score
score++;
}
} else if (index >= 18) { //it's q4
let userAns = $w(`#button${index}`).label;
if (userAns == data[3]["correct_answer"]) {
score++;
}
} else if (index >= 14) { //it's q4
let userAns = $w(`#button${index}`).label;
if (userAns == data[2]["correct_answer"]) {
score++;
}
} else if (index >= 10) { //it's q4
let userAns = $w(`#button${index}`).label;
if (userAns == data[1]["correct_answer"]) {
score++;
}
} else if (index >= 6) { //it's q4
let userAns = $w(`#button${index}`).label;
if (userAns == data[0]["correct_answer"]) {
score++;
}
}
local.setItem("score", score); //storing the final score in local storage
})
}
});