I started this project by creating all
the html forms and buttons for the quiz. Each question would have markup similar
to this:
1234567891011121314
<div id="question-1">
<h2>Where does the phrase 'dog days of summer' come from ?</h2>
<form>
<input type="radio" name="q1" id="q1-a" value="dogs" /> Lazy Dogs
<br>
<input type="radio" name="q1" id="q1-b" value="movie" /> Reference from a movie
<br>
<input type="radio" name="q1" id="q1-c" value="novel" /> A term coined from a famous novel
<br>
<input type="radio" name="q1" id="q1-d" value="star" /> A star
<br><br>
<input type="submit" value="Final answer">
</form>
</div>
The other important html markup involved elements that are initially hidden using
the css property display. They’re shown depending on which answer is chosen.
Once the app worked, I migrated the questions into Javascript
objects and worked towards making the functions reusable. I created each
question by calling the createQuestion function.
1234567891011121314
var question1 = new createQuestion("1","Where does the phrase 'dog days of summer' come from ?",
"Lazy dogs", "A reference", "A term coined from a famous novel", "A star",
"A star");
function createQuestion(id, question, a, b, c, d, answer) {
this.id = id;
this.question = question;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.answer = answer;
return this;
}
Afterwards, I added them to an array and looped through it to create the forms.
The previous css still worked and the questions are hidden until the
corresponding submit handler is called.
1234567891011121314151617181920
var questionList = [question1, question2, question3, question4, question5 ];
for (var i = 0, l = questionList.length; i < l; i ++) {
var v = questionList[i];
var result = createQuestionform(questionList[i]);
$('section').append(result);
}
function createQuestionform(obj) {
var result = $("<div id='question-"+obj.id+"'>"+
"<h2>"+obj.question+"</h2>"+
"<form>"+
"<input type='radio' name='q"+obj.id+"' value='"+obj.a+"'/>"+obj.a+"<br>"+
"<input type='radio' name='q"+obj.id+"' value='"+obj.b+"'/>"+obj.b+"<br>"+
"<input type='radio' name='q"+obj.id+"' value='"+obj.c+"'/>"+obj.c+"<br>"+
"<input type='radio' name='q"+obj.id+"' value='"+obj.d+"'/>"+obj.d+"<br><br>"+
"<input type='submit' value='Final answer'>"+
"</form>"+
"</div>");
return result;
}
I also used Pixlr to shrink the images from
Unsplash so that they would be more web
friendly.