forked from TiagoDanin/SimpleTrivia.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jquery.js
74 lines (62 loc) · 1.87 KB
/
index.jquery.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
/* eslint-disable no-undef */
let score = 0
let trival = {}
const colors = {
green: '#32d296',
red: '#f0506e'
}
const apiBase = 'https://opentdb.com/api.php?amount=1&type=multiple'
const showQuestion = () => {
$('#app')[0].hidden = false
const questions = [...trival.incorrect_answers, trival.correct_answer].sort(() => 0.5 - Math.random())
let htmlAnswers = '<h2 class="uk-card-title">Answers</h2>\n'
for (let question of questions) {
question = question.charAt(0).toUpperCase() + question.slice(1)
htmlAnswers += `<button style="margin-bottom: 3%;" type="button" class="bt-select uk-button uk-text-large">${question}</button>\n`
}
$('#answers').html(htmlAnswers)
$('#question').html(trival.question)
$('#title').html(`Category: ${trival.category} (${trival.difficulty})`)
$('.bt-select').on('click', checkAnswer)
}
const getQuestions = () => {
return $.getJSON(apiBase)
.then(data => {
if (data.response_code === 0) {
trival = data.results[0]
showQuestion()
}
}).catch(error => console.error(error))
}
const checkAnswer = async eventClick => {
$('.bt-select').off('click', checkAnswer)
for (const answer of $('.bt-select')) {
if (trival.correct_answer.toUpperCase() === answer.innerText.toUpperCase()) {
answer.style.borderColor = colors.green
answer.style.borderStyle = 'solid'
}
}
if (trival.correct_answer.toUpperCase() === eventClick.target.innerText.toUpperCase()) {
switch (trival.difficulty) {
case 'easy':
score += 5
break
case 'medium':
score += 10
break
case 'hard':
score += 20
break
default:
score += 5
}
$('#score-point').text(`Points: ${score}`)
eventClick.target.style.background = colors.green
} else {
eventClick.target.style.background = colors.red
}
eventClick.target.style.color = 'black'
await new Promise(resolve => setTimeout(resolve, 1500))
getQuestions()
}
getQuestions()