-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscripts.js
157 lines (125 loc) · 3.39 KB
/
scripts.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
// values
let questionArr = [];
let currentQuestion = null;
var correctOption = -1;
var scoreVal = 0;
var streakVal = 0;
var maxStreak = 0;
var highScore = 0;
// html elements
let categoryText = document.getElementById("category");
let questionText = document.getElementById("question");
let score = document.getElementById("score");
let streak = document.getElementById("streak");
let maxStreakDiv = document.getElementById("max-streak");
let highScoreDiv = document.getElementById("high-score");
// load questions to start
loadQuestions();
function resetInitialValues() {
scoreVal = 0;
streakVal = 0;
}
function loadQuestions() {
fetch('https://opentdb.com/api.php?amount=10&type=multiple')
.then(response => response.json())
.then(data => {
questionArr.push(...data.results);
if (!currentQuestion) {
currentQuestion = getNextValidQuestion();
updateUI();
}
$("#game-container").removeClass("hide");
}
);
}
function updateUI() {
$('input[name="answer"]').prop('checked', false);
categoryText.innerHTML = currentQuestion.category;
questionText.innerHTML = currentQuestion.question;
score.innerHTML = scoreVal;
streak.innerHTML = streakVal;
maxStreakDiv.innerHTML = maxStreak;
highScoreDiv.innerHTML = highScore;
setupOptions();
}
function setupOptions() {
let icLength = currentQuestion.incorrect_answers.length;
correctOption = randomInt(1, 1 + icLength);
$(`#label-${correctOption}`).html(currentQuestion.correct_answer);
let currIndex = 0;
for (let i = 1; i < icLength + 2; i++) {
if (i === correctOption) {
continue;
}
$(`#label-${i}`).html(currentQuestion.incorrect_answers[currIndex]);
currIndex++;
}
}
function randomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function getNextQuestion() {
let nextQ = questionArr.shift();
if (questionArr.length < 6) {
loadQuestions();
}
return nextQ;
}
function getNextValidQuestion() {
let nextQ = getNextQuestion();
while (!isValidQuestion(nextQ)) {
nextQ = getNextQuestion();
}
return nextQ;
}
function isValidQuestion(q) {
return q.correct_answer && q.question
}
// handles selection of radio option
$('input[name="answer"]').on('click', function(e) {
let selected = $("input[type='radio']:checked");
let selectedVal = selected.val();
if (isCorrect(selectedVal)) {
var pointVal = 0;
switch(currentQuestion.difficulty) {
case "easy":
pointVal = 1;
break;
case "hard":
pointVal = 5;
break;
default:
pointVal = 3;
break;
}
scoreVal+=pointVal;
streakVal++;
updateHighScores();
$(`#label-${selectedVal}`).addClass("correct");
} else {
resetInitialValues();
$(`#label-${selectedVal}`).addClass("wrong");
}
$('input[name=answer]').attr("disabled",true);
});
function isCorrect(a) {
return a == correctOption;
}
function updateHighScores() {
if (scoreVal > highScore) {
highScore = scoreVal;
}
if (streakVal > maxStreak) {
maxStreak = streakVal;
}
}
// callback triggered when css animation finished
for (let i = 1; i < 5; i++) {
let optionLabel = $(`#label-${i}`);
optionLabel.on('animationend', function(e) {
optionLabel.removeClass("correct wrong");
currentQuestion = getNextValidQuestion();
updateUI();
$('input[name=answer]').attr("disabled",false);
});
}