Skip to content

Instantly share code, notes, and snippets.

@dannysperry
Created July 12, 2013 21:26
Show Gist options
  • Save dannysperry/5987997 to your computer and use it in GitHub Desktop.
Save dannysperry/5987997 to your computer and use it in GitHub Desktop.
Javascript examples from questions.js in my quiz repo
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1);
};
var questions = function() {
// clear all, if any, questions within .QUESTION
$('.question').html('');
// create each question form and append to .QUESTION
$.each(allQuestions, function(i){
// create html for individual questions and choices
var html = "<form id = question_" + (i+1) + "><p>" + allQuestions[i].question + "</p>";
// loop through and create radio buttons for each choice
for(var x = 0 ; x < allQuestions[i].choices.length; x++){
html += "<input class='choice' id='choice" + (i+1) + "_" + (x+1) + "' type='radio' name='radio" + (i+1) + "' class='radio' value='" + allQuestions[i].choices[x] + "' >";
html += "<label for='choice" + (i+1) + "_" + (x+1) + "'>" + allQuestions[i].choices[x].capitalize() + "</label><br>";
}
//make a previous button only if there is a question to go back to
if (i > 0) {html += "<div class='previous'>Previous</div><br>";}
html += "<div class='next'>Next</div></form>";
// insert html for next individual question
$('.question').append(html);
});
};
var answer = function(i) {
var checked = $('#question_'+ (i) +' input:checked');
// client side validation: make sure a choice is checked before moving to next question
if(checked.length !== 0){
// submit a 1 or 0 into the CORRECT array if answer is correct
if (checked.val().toLowerCase() === allQuestions[i-1].correctAnswer.toLowerCase() ){
correct[i-1] = 1;
} else {
correct[i-1] = 0;
}
} else {
return false;
}
};
// fade out current question and fade in next question
var forward = function(i) {
$('#question_' + (i)).fadeOut(function(){
$('#question_' + (i+1)).fadeIn();
});
qCount += 1;
};
// fade out current question and fade in previous question
var backward = function(i){
$('#question_' + (i)).fadeOut(function(){
$('#question_' + (i-1)).fadeIn();
});
qCount -= 1;
};
var next = function(i) {
// if no question is displayed, display the first question
if(i === 0) {
begin();
// as long as a choice is selected
} else if (answer(i) !== false){
// if no more questions remain, show results
if(i === (l)){
end(i);
// if more questions remain, show next question
} else {
forward(i);
}
}
};
$(document).keypress(function(e) {
// if letter "c" is pressed
if(e.which === 99) {
// loop through each question
$.each(allQuestions, function(i){
// loop through each choice within current question
$.each(allQuestions[i].choices, function(x){
// if current choice within current question is equal to current questions correctAnswer value
if(allQuestions[i].choices[x] === allQuestions[i].correctAnswer){
// select that specific choices <label> element and store in variable
var correct = $("label[for='choice" + (i+1) + "_" + (x+1) + "']");
// get current background-color
var bgColor = correct.css("background-color");
// clear any previous button presses and animate the background with a flash of yellow
correct.clearQueue().animate({ backgroundColor: "yellow" }, 500).animate({ backgroundColor: bgColor }, 1000);
}
});
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment