-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (59 loc) · 2.38 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
function decodeHtml(html) {
var txt = document.createElement("textarea");
txt.innerHTML = html;
return txt.value;
}
window.onload = async () => {
const responseText = document.querySelector("#response-msg");
const trueBtn = document.querySelector("#true");
const questionText = document.querySelector("#question");
const nextQuestion = document.querySelector("#next-question");
const falseBtn = document.querySelector("#false");
const score = document.querySelector("#score");
const putNewQuestionOnPage = async (totalCounter, correctCounter) => {
const response = await axios.get(
"https://opentdb.com/api.php?amount=1&category=18&type=boolean"
);
console.log("totalCounter", totalCounter, "correctCounter", correctCounter);
const question = response.data.results[0].question;
questionText.textContent = decodeHtml(question);
const correctAnswer = response.data.results[0].correct_answer;
trueBtn.onclick = () => {
if (correctAnswer === "True") {
nextQuestion.onclick = async () => {
await putNewQuestionOnPage(totalCounter + 1, correctCounter + 1);
};
responseText.textContent = "Correct!";
// document.location.reload(); // debugging
} else {
nextQuestion.onclick = async () => {
await putNewQuestionOnPage(totalCounter + 1, correctCounter);
};
responseText.textContent = "Sorry!";
// document.location.reload(); // debugging
}
};
// const refreshBtn = document.querySelector("#my-button");
// refreshBtn.onclick = () => document.location.reload();
falseBtn.onclick = () => {
if (correctAnswer === "False") {
nextQuestion.onclick = async () => {
await putNewQuestionOnPage(totalCounter + 1, correctCounter + 1);
};
responseText.textContent = "Correct!";
// document.location.reload(); // debugging
} else {
nextQuestion.onclick = async () => {
await putNewQuestionOnPage(totalCounter + 1, correctCounter);
};
responseText.textContent = "Sorry!";
// document.location.reload(); // debugging
}
};
score.textContent = `You have answered ${correctCounter} out of ${totalCounter} correct!`;
nextQuestion.onclick = async () => {
await putNewQuestionOnPage(totalCounter + 1, correctCounter);
};
};
await putNewQuestionOnPage(0, 0);
};