I'm following this guide: http://javascriptissexy.com/how-to-learn-javascript-properly/
Currently, I'm trying to make the Dynamic Quiz detailed further down that page. The idea: loop past all the questions, and for every question in the array, create a new form, wait for it to be submitted, process the submitted information, and continue to the next loop interation. I am currently having trouble, in that I don't know how to pause the loop to wait for the form to be submitted. How do I do this? See the below code, and the function "presentQuestion()" specifically.
$(document).ready(function () {
var questionArray = [
{ question: "The Netherlands", choices: ["Amsterdam", "Monaco", "Chisinau"], correctAnswer: 0 },
{ question: "Austria", choices: ["Bern", "Vienna", "Copenhagen"], correctAnswer: 1 },
{ question: "Bulgaria", choices: ["Budapest", "Sofia", "Minsk"], correctAnswer: 1 },
{ question: "Sweden", choices: ["Stockholm", "Malmo", "Gothenburg"], correctAnswer: 0 },
{ question: "Latvia", choices: ["Vilnius", "Skopje", "Riga"], correctAnswer: 2 },
{ question: "Lithuania", choices: ["Riga", "Skopje", "Vilnius"], correctAnswer: 2 }];;
var score;
var questionField = $("#questionDiv");
var tallyField = $("#tallyDiv");
$("#startButton").on("click", function () {
resetTally();
askQuestions();
});
$("#answerButton").on("click", function () {
// The button from the generated question form.
// When pressed, call evaluateAnswer()
})
function askQuestions() {
for (var i = 0; i < questionArray.length; i++) {
questionField.empty();
presentQuestion(questionArray[i]);
}
}
function presentQuestion(q) {
questionField.append("What is the capital of " + q.question + "?");
questionField.append('<form method="POST">');
for (var i = 0; i < q.choices.length; i++) {
questionField.append(q.choices[i] + ' <input type="radio" name="answers" id="' + i + '"/></p>')
}
questionField.append('<input type="submit" id="answerButton" value="submit answer"/>');
// to do: code that pauses the askQuestions() for loop
}
function evaluateAnswer() {
// to do: take the submitted form info and evaluate the given answer. If correct, send
// feedback to updateTally()
}
function updateTally(givenAnswer) {
if (givenAnswer == true) {
// to do: increase score by 1, loop iteration ends and the next iteration
// in askQuestions() starts
}
else {
// Same, but with a score increase of 0
}
}
function resetTally() {
tallyField.text("Start answering!");
score = 0;
}
});