Quiz Fiddle Pt.II
JS for the Quiz App enhanced version
by Alan Killian
HTML
<div>
<h2>
SHS Quiz App
</h2>
<div id="container">
<p id="question">
Your Question Will Appear Here.</p>
<br/>
<form id="test1"></form>
</div>
<br/>
</div>
<div id="div2">
<button id="next">Click here for Question</button>
</div>
JavaScript
$(document).ready(function() {
//Array of array to hold Test Questions and answers
var allQuestions = [{
question: ["Who is Prime Minister of the United Kingdom?", "Who is the Vice-President of the United States?", "Who is Chancellor of Germany?", "Who is the Prime Minister of Canada?"],
choices: [
["David Cameron", "Gordon Brown", "Winston Churchill", "Tony Blair"],
["Barack Obama", "Bernie Sanders", "Nancy Pelosi", "Joe Biden"],
["Franz Ritz", "Angela Merkel", "Jan Schroeder", "John Stevens", "Karl Marx"],
["Wayne Gretsky", "Pierre Trudeau", "Mike Myers", "Justin Trudeau", "Justin Bieber"]
],
correctAnswer: [0, 3, 1, 3]
}];
////Initialize Variables
var questionCounter = 0;
var answerTally = 0;
var score = 0;
var submitAnswerClick = false;
var i = questionCounter;
var noSelection = false;
//Check for correct answer and keep tally of total correct answers function.
function check_answer() {
for (var k = 0; k < allQuestions[0].choices[i].length; k++) {
//Check if radio button is marked and answer is correct
if ((document.getElementById(k).checked == true) && (document.getElementById(k).value == allQuestions[0].correctAnswer[questionCounter])) {
//If so, add to tally and increment counters
answerTally = answerTally + 1;
questionCounter = questionCounter + 1;
i = questionCounter;
return answerTally;
} else if (document.getElementById(k).checked == true) {
questionCounter = questionCounter + 1;
i = questionCounter;
return answerTally;
} else {
//we didnt check a radio button
if (k + 1 == allQuestions[0].choices[i].length) {
noSelection = true;
alert("Alert. No Box Checked.");
return noSelection;
}
}
}
}
//End check_Answer function
// Begin Render Prev/Next Button function
function button_Render() {
if (questionCounter == 0) {
...