-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
155 lines (131 loc) · 4.84 KB
/
main.js
File metadata and controls
155 lines (131 loc) · 4.84 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
// Initialize Game Board on Load
initBoard()
initCatRow()
document.querySelector('button').addEventListener('click', buildCategoriesRow)
function initCatRow(){
let catRow = document.getElementById('category-row')
for (let k=0; k<6; k++){
let box= document.createElement('div')
box.className= 'clue-box category-box'
catRow.appendChild(box)
}
}
function getClue(){
console.log('Time for a clue!')
}
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)
}
}
//Declare Random Integer
function randInt(){
return Math.floor(Math.random() * (20418) + 1)
}
//FETCH responses from API
let catArray= []
function buildCategoriesRow(){
if (!(document.getElementById('category-row').firstChild.innerText== '')){
resetBoard()
}
const fetchRequest1 = fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const fetchRequest2= fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const fetchRequest3= fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const fetchRequest4= fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const fetchRequest5= fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const fetchRequest6= fetch(
`https://jservice.io/api/category?&id=${randInt()}`)
.then((response)=>response.json());
const allQuestions= Promise.all([fetchRequest1,fetchRequest2,fetchRequest3,fetchRequest4,fetchRequest5,fetchRequest6])
allQuestions.then((response)=>
{console.log(response)
catArray= response
setCategories(catArray)
})
}
//Use API to set Category Titles
function setCategories(catArray){
let element = document.getElementById('category-row')
let children= element.children;
for (let x=0; x<children.length; x++){
children[x].innerHTML=catArray[x].title
}
}
//Pinpoint which box was clicked!
function getClue (event){
let child=event.currentTarget
console.log(child)
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
})
showQuestion(clue, child, boxValue)
}
// SHOW the quesiton and get player's answer
function showQuestion(clue, target, boxValue){
let userAnswer = prompt(clue.question).toLowerCase()
let answer = clue.answer.toLowerCase().replace(/<\/?[^>]+(>\$)/g, "")
let possiblePoints= +(boxValue)
target.innerHTML=clue.answer
target.removeEventListener('click', getClue, false)
evaluateAnswer(userAnswer, answer, possiblePoints)
}
//Evaluate Correct Answer!
function evaluateAnswer(userAnswer, answer, possiblePoints){
let checkAnswer =(userAnswer==answer)? 'correct' : 'incorrect'
let confirmAnswer=
confirm(`For $${possiblePoints}, you answered "${userAnswer}". The correct answer was "${answer}. Your answer appears to be $${checkAnswer}. Click OK to accept or click Cancel if th eanswer 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 given.`)
}
}
// Reset Board 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()
}