-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
234 lines (205 loc) · 6.63 KB
/
index.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
226
227
228
229
230
231
232
233
'use strict';
let questionNumber = 0;
let score = 0;
const QAs = [
{
question: "What does kerning mean?",
answers: ["the space between words", "the space between letters", "the space between lines", "the central kernel of your design"],
correctAnswer: "the space between letters"
},
{
question: "Which is NOT a proper file format for raster art?",
answers: ["TIFF", "JPG", "EPS", "SVG"],
correctAnswer: "SVG"
},
{
question: "What does K in CMYK mean?",
answers: ["key", "kit", "kick", "kerning"],
correctAnswer: "key"
},
{
question: "Which color space should you design something for print?",
answers: ["RGB", "LAB", "CMYK", "gray scale"],
correctAnswer: "CMYK"
},
{
question: "What does leading mean?",
answers: ["the space between words", "the space between letters", "the space between lines", "when you're design is the leader"],
correctAnswer: "the space between lines"
},
{
question: "How many fonts should you use in a design?",
answers: ["only one", "three or less", "as many as you want", "five"],
correctAnswer: "three or less"
},
{
question: "Which font is a serif font?",
answers: ["Arial", "Gotham", "Helvetica", "Times New Roman"],
correctAnswer: "Times New Roman"
},
{
question: "Which program is best suited to create Vector art?",
answers: ["Illustrator", "Photoshop", "Dreamweaver", "Indesign"],
correctAnswer: "Illustrator"
},
{
question: "Which term describes how much gray is in a color?",
answers: ["hue", "alpha", "saturation", "luminosity"],
correctAnswer: "saturation"
},
{
question: "What is the primary goal of a graphic designer?",
answers: ["to design something unique", "to typeset", "to communicate a message effectively", "to design something beautiful"],
correctAnswer: "to communicate a message effectively"
}
];
//Get the total number of questions
Object.size = function(obj) {
// var size = 0, key;
// for (key in obj) {
// if (obj.hasOwnProperty(key)) size++;
// }
return (QAs.length);
// return size;
};
let numQs = Object.size(QAs);
function renderQuestions(qn) {
let newHtml=""
for (let i=0; i<(QAs[qn].answers.length); i++) {
newHtml = newHtml+(`
<div class="col-6">
<label class="container">
<input type="radio" name="answerOption" required value="${QAs[qn].answers[i]}">
${QAs[qn].answers[i]}
</input>
<span class="checkmark"></span>
</label>
</div>`);
}
let returnHtml = `
<form name="js-quiz" id="js-quiz" action="">
<div class="qandabox row">
<div class="col-6 column question left">
<p>${QAs[qn].question}</p>
</div>
<div class="col-6 column right">
<div class="row">`+newHtml+`
<div class="col-6">
<button type="submit" value="Submit Answer" class="button submitanswer"><span>Submit</span></input>
</div>
</div>
</div>
</div>
</form>`;
return returnHtml;
}
//update function
function updateOutput(htmlSelection, item) {
return $(htmlSelection).html(item);
}
//increment function
function handleIncrement(item) {
let i = item;
i = i+1;
return i;
}
//increment score number and update html output
function handleScore() {
score = handleIncrement(score);
updateOutput(".score", score); //or $(".score").text(score); works
}
//increment question number and update html output
function handleQuestionNumber() {
questionNumber = handleIncrement(questionNumber);
updateOutput(".questionNumber", questionNumber);
}
//handle num of qs to go on score and question (2/10(10 or however many Qs))
function handleNumQs() {
updateOutput(".numQs", numQs);
}
function handleStartButton() {
$('.mainArea').on('click', '#js-start-Button', function (event) {
handleQuestionNumber();
updateOutput(".mainArea", (renderQuestions(questionNumber-1)));
});
}
function handleNextQuestion () {
$('.mainArea').on('click', '#js-next-question', function (event) {
if (questionNumber < numQs){ //or QAs.length works
handleQuestionNumber();
updateOutput(".mainArea", (renderQuestions(questionNumber-1)));
}
else {
generateResults();
}
});
}
function handleSubmitAnswer() {
$('.mainArea').submit('.submitButton', function(event) {
event.preventDefault();
let userAnswer = checkAnswer();
if (userAnswer === true) {
handleScore();
generateCorrectFeedback();
}
else {
generateWrongFeedback();
}
});
}
function checkAnswer() {
if (($('input[name=answerOption]:checked').val()) === (QAs[questionNumber-1].correctAnswer)) {
return true;
}
else {
return false;
}
}
function generateCorrectFeedback(){
let cfeedback = "<p>Awesome job... you are correct!</p><button id=\"js-next-question\" class=\"button\"><span>Next</span></button>";
$( ".mainArea" ).html(cfeedback);
}
function generateWrongFeedback(){
let wfeedback = "<p>Wrong answer. The correct answer is "+(QAs[questionNumber-1].correctAnswer)+".</p><button id=\"js-next-question\" class=\"button\"><span>Next</span></button>";
$( ".mainArea" ).html(wfeedback);
}
function generateResults() {
let finalScore = (score/numQs)*100;
let final="";
if (finalScore < 50) {
final = final+"Nice try but keep studying."
}
else if (finalScore > 100) {
final = final+"Perfect score! You've got the knowledge to be a fantastic designer!"
}
else {
final = final+"Not perfect but great job!"
}
let results = " You got "+score+" out of "+numQs+" questions.<br/> "+final+"<br/><button id=\"js-restart-button\" class=\"button\"><span>Restart Quiz</span></button>";
updateOutput(".mainArea", results);
}
function handleRestartButton() {
$('.mainArea').on('click', '#js-restart-button', function (event) {
score = 0;
questionNumber = 0;
updateOutput(".score", score);
updateOutput(".questionNumber", questionNumber);
generateStartingHTML();
});
}
function generateStartingHTML(){
let startingHTML = `<p>Got the basics down to make a great designer? Let's find out!</p>
<button type="button" id="js-start-Button" class="button center" style="vertical-align:middle"><span>Start Quiz</span></button>`;
$( ".mainArea" ).html(startingHTML);
}
//callback function for app's document ready
function handleQuiz() {
generateStartingHTML();
handleStartButton();
handleSubmitAnswer();
handleNextQuestion();
handleNumQs();
handleRestartButton();
};
//when the page loads, call `handleQuiz`
$(handleQuiz);