-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
214 lines (178 loc) · 4.59 KB
/
Copy pathindex.js
File metadata and controls
214 lines (178 loc) · 4.59 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
'use strict';
const questionSet = [
{
number: 1,
text: 'What is one of the Cs to consider when selecting a ring?',
ans: [
'Care',
'Comparison',
'Color',
'Confidence'],
},
{
number: 2,
text: 'What is another name for a cushion cut?',
ans: [
'Pear',
'Square',
'Oval',
'Old mine cut'],
},
{
number: 3,
text: 'What is a round setting called?',
ans: [
'Halo',
'Bezel',
'Prong',
'Solitaire'],
},
{
number: 4,
text: 'What type of metal is the pink tinted one called?',
ans: [
'Platinum',
'Rose Gold',
'Gold',
'Sterling Silver'],
},
{
number: 5,
text: 'How do you know if your diamond is well cut?',
ans: [
'Larger in size',
'It reflects more light',
'Dull',
'Smaller in size'],
}
];
const ANSWERS = [
'Color',
'Old mine cut',
'Halo',
'Rose Gold',
'It reflects more light',
];
let questionNum = 1;
let correctAnswers = 0;
function questionTemplate(correctAnswers, question, questionsAnswered) {
return `
<form class="template" role="form">
<fieldset>
<legend id="question-page">
<h2 id="question">${question.text}</h2>
</legend>
<label class="answerChoice">
<input class="answer" type="radio" name="option" checked required></input>
<span>${question.ans[0]}</span>
</label>
<label class="answerChoice">
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans[1]}</span>
</label>
<label class="answerChoice">
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans[2]}</span>
</label>
<label class="answerChoice">
<input class="answer" type="radio" name="option" required></input>
<span>${question.ans[3]}</span>
</label>
</fieldset>
<button type "submit" id="js-submit-button">Submit</button>
</form>
<div id="status-bar">
<span id="question-count">Question: ${question.number}/5</span>
<span id="score-count">Score: ${correctAnswers}/${questionsAnswered}</span>
</div>
</section>
`;
}
function handleStartButton() {
$('#js-start-button').click(function(event) {
nextQuestion();
});
}
function handleSubmitButton() {
$('#container').on('click', '#js-submit-button', function(event) {
event.preventDefault()
const answer = $('input:checked').siblings('span');
const userIsCorrect = checkUserAnswer(answer);
if(userIsCorrect) {
generateCorrectFeedback();
} else {
generateIncorrectFeedback();
}
});
}
function handleNextButton() {
$('#container').on('click', '#js-next-button', function(event) {
if(questionNum === 5) {
createResultsPage(correctAnswers);
} else {
iterateQuestion();
nextQuestion();
}
});
}
function handleRestartButton() {
$('#container').on('click', '#js-restart-button', function(event) {
questionNum = 1;
correctAnswers = 0;
nextQuestion();
});
}
function nextQuestion() {
const question = questionSet[questionNum - 1];
const questionsAnswered = questionNum - 1;
$('#container').html(questionTemplate(correctAnswers, question, questionsAnswered));
}
function checkUserAnswer(answer) {
if(answer.text() === ANSWERS[questionNum - 1]) {
return true;
} else {
return false;
}
}
function generateCorrectFeedback() {
$('#container').html(correctFeedback);
iterateCorrectAnswers();
}
const correctFeedback = `
<section class="feedback-page" role="main">
<h2>You are Correct!</h2>
<button type="button" id="js-next-button">Next</button>
</section>
`;
function generateIncorrectFeedback() {
$('#container').html(incorrectFeedbackTemplate(questionNum));
}
function incorrectFeedbackTemplate(questionNum) {
return `
<section class="feedback-page" role="main">
<h2>Wrong! It was ${ANSWERS[questionNum - 1]}!</h2>
<button id="js-next-button">Next</button>
</section>
`;
}
function iterateQuestion() {
questionNum++;
}
function iterateCorrectAnswers() {
correctAnswers++;
}
function createResultsPage(correctAnswers) {
$('#container').html(`
<section id="final-page" role="main">
<h2>Final Score: ${correctAnswers} out of 5</h2>
<button type="button" id="js-restart-button">Try Again?</button>
</section>
`);
}
function handleButtons() {
handleStartButton();
handleSubmitButton();
handleNextButton();
handleRestartButton();
}
handleButtons();