-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
225 lines (190 loc) · 5.97 KB
/
script.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
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
219
220
221
222
223
224
225
/**
* Example store structure
*/
const store = {
// 5 or more questions are required
questions: [
{
question: 'Which instrument with 4 strings plays music written in the treble clef?',
answers: [
'bass',
'viola',
'violin',
'cello'
],
correctAnswer: 'violin'
},
{
question: 'Which instrument has 6 strings and often plays arpeggiated chords?',
answers: [
'violin',
'bass',
'guitar',
'harp'
],
correctAnswer: 'guitar'
},
{
question: 'Which instrument has many strings, but is often grouped with percussion because of the rhythmic quality of its music?',
answers: [
'piano',
'cello',
'violin',
'tuba'
],
correctAnswer: 'piano'
},
{
question: 'What medieval instrument is the ancestor to guitars and others?',
answers: [
'lyre',
'cello',
'mandolin',
'lute'
],
correctAnswer: 'lute'
},
{
question: 'This is the best stringed instrument of all time, obviously.',
answers: [
'bass',
'bass',
'bass',
'bass'
],
correctAnswer: 'bass'
}
],
quizStarted: false,
questionNumber: 0,
score: 0
};
/**
*
* Technical requirements:
*
* Your app should include a render() function, that regenerates the view each time the store is updated.
* See your course material and access support for more details.
*
* NO additional HTML elements should be added to the index.html file.
*
* You may add attributes (classes, ids, etc) to the existing HTML elements, or link stylesheets or additional scripts if necessary
*
* SEE BELOW FOR THE CATEGORIES OF THE TYPES OF FUNCTIONS YOU WILL BE CREATING 👇
*
*/
/********** TEMPLATE GENERATION FUNCTIONS **********/
// These functions return HTML templates
function startPage() {
// generates html for the intro page with a start button
return `<h2>Welcome to the Quiz!</h2><p>Please click START</p><button id="srtBtn">START</button>`
}
function gamePlay() {
//generate html and questions and answer choices from .store
let qNum = store.questionNumber
return `<h2>Question # ${qNum + 1} / ${store.questions.length}</h2><p>${store.questions[qNum].question}</p>
<form><fieldset class="ansOpt">
<div id="ansBtn">
<button class="answer" id="0" value="0">${store.questions[qNum].answers[0]}</button>
<button class="answer" id="1" value="1">${store.questions[qNum].answers[1]}</button>
<button class="answer" id="2" value="2">${store.questions[qNum].answers[2]}</button>
<button class="answer" id="3" value="3">${store.questions[qNum].answers[3]}</button></div></fieldset></form>`
}
function wrongRight(answer) {
//html for 'correct' choice or incorrect and gives correct answer
if (answer === store.questions[store.questionNumber].correctAnswer ) {
$('#main').append(`<div class="ansCrt">CORRECT!</div>`)
store.score++
}else {
$('#main').append(`<div class="ansInc">INCORRECT!</div><P>The correct answer is: ${store.questions[store.questionNumber].correctAnswer}!</p>`)
}
nextButton()
}
function nextButton() {
//creates next button or results button
if (store.questionNumber < store.questions.length -1) {
$('#main').append(`<button class="next">NEXT QUESTION</button>`)
}else if (store.questionNumber = store.questions.length -1) {
$('#main').append(`<button class="results">RESULTS</button>`)
store.questionNumber += 2
}
clickNextBtn()
ppltResults()
}
function runningTotal() {
//html for banner that shows store.questionNumber and store.score
if (store.questionNumber > 0) {
return `<div id="score-banner">Score: ${store.score}/${store.questionNumber}<div>`
}else {
return ``
}
}
function gameOver() {
//give total score and make 'restart' button
let pctCrt = (store.score/store.questions.length)
if (pctCrt <= .2) {
return `<h2>You scored ${store.score} out of ${store.questions.length}</h2><p>You could do better.</p><button class="rstBtn">TRY AGAIN</button>`
}else if (pctCrt > .2 && pctCrt <= .8) {
return `<h2>You scored ${store.score} out of ${store.questions.length}</h2><p>Not bad! Could be better...</p><button class="rstBtn">TRY AGAIN</button>`
}else if (pctCrt > .8) {
return `<h2>You scored ${store.score} out of ${store.questions.length}</h2><p>Wow! Nice work!</p><button class="rstBtn">TRY AGAIN</button>`
}
}
/********** RENDER FUNCTION(S) **********/
function render() {
let currentCode = ""
if (store.quizStarted === false) {
$('#main').html(startPage())
return
}else if (store.questionNumber >= 0 && store.questionNumber < store.questions.length) {
currentCode = runningTotal()
currentCode += gamePlay()
$('#main').html(currentCode)
}else {
$('#main').html(gameOver())
reset()
}
}
// This function conditionally replaces the contents of the <main> tag based on the state of the store
/********** EVENT HANDLER FUNCTIONS **********/
// These functions handle events (submit, click, etc)
function clickStart() {
$('#srtBtn').on('click', (event) => {
store.quizStarted = true
render()
submitAnswer()
})
}
function submitAnswer() {
$('.answer').on('click', (event) => {
event.preventDefault()
wrongRight(event.target.innerHTML)
$('#ansBtn').hide()
})
}
function clickNextBtn() {
$('.next').on('click', (event) => {
store.questionNumber++
render()
submitAnswer()
})
}
function ppltResults() {
$('.results').on('click', (event) => {
render()
})
}
function reset() {
$('.rstBtn').on('click', (event) => {
store.quizStarted = false
store.questionNumber = 0
store.score = 0
start()
})
}
function start() {
render()
clickStart()
ppltResults()
}
$(start)