-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
186 lines (148 loc) · 5.65 KB
/
script.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
var url;
var data;
var quiz = [];
var header = document.querySelector('header');
var points = 0;
let h2 = document.createElement('h2');
let answerArea = document.getElementById('answers');
var iterator = 0;
var answers = [];
var correctAnswer = "";
var progressBar = document.querySelector('progress');
progressBar.value = 1;
// Initialisation by setting up listeners for the selectors and the introductory text.
document.getElementById('startbutton').onclick = play;
var text = document.createElement('h3');
text.innerText = 'Welcome! \n Please select the amount of questions you want to answer and the level of difficulty. ' +
'\n Then press the button to start. You can at any time press the restart button to restart the quiz.';
header.appendChild(text);
var questionSelector = document.getElementById('numberOfQuestions');
var amount;
questionSelector.addEventListener('change', function (e) {
amount = questionSelector[questionSelector.selectedIndex].value;
});
var levelSelector = document.getElementById('level');
var level;
levelSelector.addEventListener('change', function (e) {
level = levelSelector[levelSelector.selectedIndex].value;
});
// Puts together the url and calls the next function which sets the game in motion.
// Then it restarts the game whenever the button is pressed.
function play(){
url = `https://opentdb.com/api.php?amount=${amount}&difficulty=${level}&type=multiple`;
getData(url);
progressBar.max = amount;
document.getElementById('startbutton').innerText = 'Restart';
resetData();
}
// Sends Http request to the url, creates and array with the question objects
// and calls the renderData func to take care of the data
function getData(url) {
text.innerText = "";
const xhr = new XMLHttpRequest;
xhr.onreadystatechange = function() {
if(xhr.readyState == 4){
if(xhr.status == 200){
console.log("Status: " + xhr.status);
data = xhr.response;
quiz = data.results;
renderData(quiz[iterator]);
}
}
};
xhr.open('GET', url, true);
xhr.responseType = 'json';
xhr.send();
}
// Receives the array with the answers, loops through them and sets them in buttons
function assignButtons(answers) {
answers.forEach( (answer)=> {
var answerButton = document.createElement('div');
answerButton.innerHTML = `<div class="column">
<button class="answer">${answer}</button>
</div>`;
answerArea.appendChild(answerButton);
});
}
// Renders the question text and creates an array with all the answers which then forwards to
// the assignButtons func
function renderData(quizQuestion) {
h2.innerHTML = quizQuestion.question;
header.appendChild(h2);
correctAnswer = quizQuestion.correct_answer;
answers.push(correctAnswer);
quizQuestion.incorrect_answers.forEach((answer) => {
answers.push(answer);
});
answers.sort(function (a, b) {
return 0.5 - Math.random();
});
assignButtons(answers);
}
// Helper function which decodes the string e.g replace the "
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
// Helper function that composes a response for the user
function giveReply(x) {
var reply = document.createElement('h4');
if (x == 'y') {
reply.innerHTML = 'Correct! Good job!';
} else {
reply.innerHTML = 'Wrong answer. The correct answer is ' + quiz[iterator].correct_answer;
}
answerArea.appendChild(reply);
}
// Listens for clicks on the buttons
answerArea.addEventListener('click', function (e) {
if (e.target.nodeName == 'BUTTON') {
answers = [];
// Compares the pressed button's text and returns the appropriate answer
if (e.target.innerText == decodeHtml(quiz[iterator].correct_answer)){
e.target.parentNode.classList.toggle('correct');
e.target.classList.add('correct');
e.target.parentNode.classList.toggle('anwser');
points++;
giveReply('y');
setTimeout(()=> {
e.target.classList.toggle('correct');
e.target.classList.toggle('anwser');
}, 1000)
} else {
e.target.classList.add('wrong');
e.target.parentNode.classList.toggle('wrong');
giveReply('n');
setTimeout(()=> {
e.target.classList.toggle('wrong');
}, 1000)
}
// After a reply is given, it goes to the next question by increasing the iterator and
// starts the new round. If it is the end of the quiz it presents the result to the player
if (iterator == quiz.length - 1) {
setTimeout(()=> {
answerArea.innerText = "";
h2.innerText = "You have made it to the end. Well done! You got " + points +
" correct answers out of " + quiz.length + "\n To play again, make your selections and press restart.";
}, 2700)
}else {
iterator++;
setTimeout(()=> {
answerArea.innerText = "";
progressBar.value++;
renderData(quiz[iterator]);
}, 2700)
}
}
});
// Deletes the data from the previous round before the new round begins.
function resetData() {
iterator = 0;
answers = [];
quiz = [];
points = 0;
h2.innerText = "";
answerArea.innerText = "";
progressBar.value = 1;
}