-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
167 lines (129 loc) · 4.77 KB
/
main.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
//INITIALIZE THE GAME BOARD ON PAGE LOAD
initCatRow()
initBoard()
document.querySelector('button').addEventListener('click',buildCategories)
//CREATE CATEGORY ROW
function initCatRow() {
let catRow = document.getElementById('category-row')
for (let i=0; i<6; i++) {
let box = document.createElement('div')
box.className = 'clue-box category-box'
catRow.appendChild(box)
}
}
//CREATE CLUE BOARD
function initBoard() {
let board = document.getElementById('clue-board')
//GENERATE 5 ROWS, THEN PLACE 6 BOXES IN EACH ROW
for (let i = 0; i < 5; i++) {
let row = document.createElement('div')
let boxValue = 200 * (i + 1)
row.className = 'clue-row'
for (let j=0; j<6; j++) {
let box = document.createElement('div')
box.className = 'clue-box'
box.textContent = '$' + boxValue
//box.appendChild( document.createTextNode(boxValue) ) //backwards compatible
box.addEventListener('click',getClue, false)
row.appendChild(box)
}
board.appendChild(row)
}
}
//CALL API
function randInt() {
return Math.floor(Math.random() * (18418) + 1)
}
let catArray = []
function buildCategories () {
if(!(document.getElementById('category-row').firstChild.innerText == '')) {
resetBoard()
}
const fetchReq1 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const fetchReq2 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const fetchReq3 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const fetchReq4 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const fetchReq5 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const fetchReq6 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`
).then((res) => res.json());
const allData = Promise.all([fetchReq1,fetchReq2,fetchReq3,fetchReq4,fetchReq5,fetchReq6])
allData.then((res) => {
console.log(res)
catArray = res
setCategories(catArray)
})
}
//RESET BOARD AND $$ AMOUNT IF NEEDED
function resetBoard() {
let clueParent = document.getElementById('clue-board')
while (clueParent.firstChild) {
clueParent.removeChild(clueParent.firstChild)
}
let catParent = document.getElementById('category-row')
while (catParent.firstChild) {
catParent.removeChild(catParent.firstChild)
}
document.getElementById('score').innerText = 0
initBoard()
initCatRow()
}
//LOAD CATEGORIES TO THE BOARD
function setCategories (catArray) {
let element = document.getElementById('category-row')
let children = element.children;
for(let i=0; i<children.length; i++) {
children[i].innerHTML = catArray[i].title
}
}
//FIGURE OUT WHICH ITEM WAS CLICKED
function getClue (event) {
let child = event.currentTarget
child.classList.add('clicked-box')
let boxValue = child.innerHTML.slice(1)
let parent = child.parentNode
let index = Array.prototype.findIndex.call(parent.children, (c) => c === child)
let cluesList = catArray[index].clues
let clue = cluesList.find(obj => {
return obj.value == boxValue
})
console.log(clue)
showQuestion(clue, child, boxValue)
}
//SHOW QUESTION TO USER AND GET THEIR ANSWER!
function showQuestion(clue, target, boxValue) {
let userAnswer = prompt(clue.question).toLowerCase()
let correctAnswer = clue.answer.toLowerCase().replace(/<\/?[^>]+(>|$)/g, "")
let possiblePoints = +(boxValue)
target.innerHTML = clue.answer
target.removeEventListener('click',getClue,false)
evaluateAnswer(userAnswer, correctAnswer, possiblePoints)
}
// EVALUATE ANSWER AND SHOW TO USER TO CONFIRM
function evaluateAnswer(userAnswer, correctAnswer, possiblePoints) {
let checkAnswer = (userAnswer == correctAnswer) ? 'correct' : 'incorrect'
let confirmAnswer =
confirm(`For $${possiblePoints}, you answered "${userAnswer}", and the correct answer was "${correctAnswer}". Your answer appears to be ${checkAnswer}. Click OK to accept or click Cancel if the answer was not properly evaluated.`)
awardPoints(checkAnswer, confirmAnswer, possiblePoints)
}
// AWARD POINTS
function awardPoints(checkAnswer, confirmAnswer, possiblePoints) {
if (!(checkAnswer == 'incorrect' && confirmAnswer == true)) {
let target = document.getElementById('score')
let currentScore = +(target.innerText)
currentScore += possiblePoints
target.innerText = currentScore
} else {
alert(`No points awarded.`)
}
}