Get Programming with JavaScript - Listing 12.07

Listing 12.07 - Checking quiz answers

var getQuiz = function () { var score = 0, qIndex = 0, inPlay = true, questions, next, getQuestion, checkAnswer, submit; questions = [ { question: "What is the highest mountain in the world?", answer: "Everest" }, { question: "What is the highest mountain in Scotland?", answer: "Ben Nevis" } ]; next = function () { qIndex = qIndex + 1; if (qIndex >= questions.length) { inPlay = false; console.log("You have finished the quiz."); } }; getQuestion = function () { if (inPlay) { return questions[qIndex].question; } else { return "You have finished the quiz."; } }; checkAnswer = function (userAnswer) { if (userAnswer === questions[qIndex].answer) { console.log("Correct!"); score = score + 1; } else { console.log("No, the answer is " + questions[qIndex].answer); } }; submit = function (userAnswer) { var message = "You have finished the quiz."; if (inPlay) { checkAnswer(userAnswer); next(); message = "Your score is " + score + " out of " + qIndex; } return message; }; return { quizMe: getQuestion, submit: submit }; }; var quiz = getQuiz();

Further Adventures

Listing 12.07 - Checking quiz answers - Task 4

var getQuiz = function () { var score = 0, qIndex = 0, inPlay = true, questions, next, getQuestion, checkAnswer, submit; questions = [ { question: "What is the highest mountain in the world?", answer: "Everest", hint: "It's been the tallest, like, forever!" }, { question: "What is the highest mountain in Scotland?", answer: "Ben Nevis", hint: "It's not Ben Franklin!" } ]; next = function () { qIndex = qIndex + 1; if (qIndex >= questions.length) { inPlay = false; console.log("You have finished the quiz."); } }; getQuestion = function () { if (inPlay) { return questions[qIndex].question; } else { return "You have finished the quiz."; } }; checkAnswer = function (userAnswer) { if (userAnswer === questions[qIndex].answer) { console.log("Correct!"); score = score + 1; } else { console.log("No, the answer is " + questions[qIndex].answer); } }; submit = function (userAnswer) { var message = "You have finished the quiz."; if (inPlay) { checkAnswer(userAnswer); next(); message = "Your score is " + score + " out of " + qIndex; } return message; }; return { quizMe: getQuestion, submit: submit }; }; var quiz = getQuiz();

Listing 12.07 - Checking quiz answers - Tasks 5 & 6

var getQuiz = function () { var score = 0, qIndex = 0, inPlay = true, questions, next, getQuestion, getHint, // declare a variable checkAnswer, submit; questions = [ { question: "What is the highest mountain in the world?", answer: "Everest", hint: "It's been the tallest, like, forever!" }, { question: "What is the highest mountain in Scotland?", answer: "Ben Nevis", hint: "It's not Ben Franklin!" } ]; next = function () { qIndex = qIndex + 1; if (qIndex >= questions.length) { inPlay = false; console.log("You have finished the quiz."); } }; getQuestion = function () { if (inPlay) { return questions[qIndex].question; } else { return "You have finished the quiz."; } }; getHint = function () { if (inPlay) { return questions[qIndex].hint; } return "You have finished the quiz."; }; checkAnswer = function (userAnswer) { if (userAnswer === questions[qIndex].answer) { console.log("Correct!"); score = score + 1; } else { console.log("No, the answer is " + questions[qIndex].answer); } }; submit = function (userAnswer) { var message = "You have finished the quiz."; if (inPlay) { checkAnswer(userAnswer); next(); message = "Your score is " + score + " out of " + qIndex; } return message; }; return { quizMe: getQuestion, helpMe: getHint, submit: submit }; }; var quiz = getQuiz();

Notice how getHint doesn't use an else-clause. Because the if-block returns a value, ending the function, the second return statement will only be reached if inPlay is false.

Have a go at removing the else-clause from the getQuestion function.