-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
218 lines (194 loc) · 6.42 KB
/
app.js
File metadata and controls
218 lines (194 loc) · 6.42 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const game = document.getElementById('game')
const scoreDisplay = document.getElementById('score')
const jeopradyCategories = [
{
genre: "WHO",
questions: [
{
questions: "Who wrote Harry Potter?",
answers: ["JK Rowling", "JRR Tolkien"],
correct: "JK Rowling",
level: "easy"
},
{
questions: "Who was born on Krypton?",
answers: ["Aquaman", "Superman"],
correct: "Superman",
level: "medium"
},
{
questions: "Who designed the first car?",
answers: ["Karl Benz", "Henry Ford"],
correct: "Karl Benz",
level: "hard"
},
]
},
{
genre: "WHERE",
questions: [
{
question: 'Where is Buckingham Palace?',
answers: ['Richmond', 'London'],
correct: 'London',
level: 'easy',
},
{
question: 'Where is the Colosseum?',
answers: ['Rome', 'Milan'],
correct: 'Rome',
level: 'medium',
},
{
question: 'Where is Mount Kilamanjaro?',
answers: ['Zimbabwe', 'Tanzania'],
correct: 'Tanzania',
level: 'hard',
},
]
},
{
genre: 'WHEN',
questions: [
{
question: 'When is Christmas?',
answers: ['30th Dec', '24/25th Dec'],
correct: '24/25th Dec',
level: 'easy'
},
{
question: 'When was JFK shot?',
answers: ['1963', '1961'],
correct: '1963',
level: 'medium'
},
{
question: 'When was WW2?',
answers: ['1932', '1941'],
correct: '1941',
level: 'medium'
},
]
},
{
genre: 'WHAT',
questions: [
{
question: 'What is the capital of Saudi Arabia?',
answers: ['Jeddah', 'Riyadh'],
correct: 'Riyadh',
level: 'hard'
},
{
question: 'What do Koalas eat?',
answers: ['Straw', 'Eucalypt'],
correct: 'Eucalypt',
level: 'medium'
},
{
question: 'What is kg short for?',
answers: ['Kilojoule', 'Kilogram'],
correct: 'Kilogram',
level: 'easy'
},
]
},
{
genre: 'HOW MANY',
questions: [
{
question: 'How many players in a football team?',
answers: ['15', '11'],
correct: '11',
level: 'easy'
},
{
question: 'How many seconds in an hour',
answers: ['36000', '3600'],
correct: '3600',
level: 'medium'
},
{
question: 'How many people live in China?',
answers: ['1.1 bil', '1.4 bil'],
correct: '1.4 bil',
level: 'hard'
},
]
}
]
let score = 0
function addCategory(category) {
const column = document.createElement('div')
column.classList.add('genre-column')
const genreTitle = document.createElement('div')
genreTitle.classList.add('genre-title')
genreTitle.innerHTML = category.genre
column.appendChild(genreTitle)
game.append(column)
category.questions.forEach(question => {
const card = document.createElement('div')
card.classList.add('card')
column.append(card)
if (question.level === 'easy') {
card.innerHTML = 100
}
if (question.level === 'medium') {
card.innerHTML = 200
}
if (question.level === 'hard') {
card.innerHTML = 300
}
card.setAttribute('data-question', question.question)
card.setAttribute('data-answer-1', question.answers[0])
card.setAttribute('data-answer-2', question.answers[1])
card.setAttribute('data-correct', question.correct)
card.setAttribute('data-value', card.getInnerHTML())
card.addEventListener('click', flipCard)
})
}
jeopradyCategories.forEach(category => addCategory(category))
function flipCard() {
this.innerHTML = ""
this.style.fontSize = "15px"
this.style.lineHeight = "30px"
const textDisplay = document.createElement('div')
textDisplay.classList.add('card-text')
textDisplay.innerHTML = this.getAttribute('data-question')
const firstButton = document.createElement('button')
const secondButton = document.createElement('button')
firstButton.classList.add('first-button')
secondButton.classList.add('second-button')
firstButton.innerHTML = this.getAttribute('data-answer-1')
secondButton.innerHTML = this.getAttribute('data-answer-2')
firstButton.addEventListener('click', getResult)
secondButton.addEventListener('click', getResult)
this.append(textDisplay, firstButton, secondButton)
const allCards = Array.from(document.querySelector('.card'))
allCards.forEach(card => card.removeEventListener('click', flipCard))
}
function getResult() {
const allCards = Array.from(document.querySelectorAll('.card'))
allCards.forEach(card => card.addEventListener('click', flipCard))
const cardOfButton = this.parentElement
if(cardOfButton.getAttribute('data-correct') == this.innerHTML) {
score = score + parseInt(cardOfButton.getAttribute('data-value'))
scoreDisplay.innerHTML = score
cardOfButton.classList.add('correct-answer')
setTimeout(() => {
while (cardOfButton.firstChild) {
cardOfButton.removeChild(cardOfButton.lastChild)
}
cardOfButton.innerHTML = cardOfButton.getAttribute('data-value')
}, 100)
} else {
cardOfButton.classList.add('wrong-answer')
setTimeout(() => {
while (cardOfButton.firstChild) {
cardOfButton.removeChild(cardOfButton.lastChild)
}
cardOfButton.innerHTML = 0
}, 100)
}
cardOfButton.removeEventListener('click', flipCard)
}