Listing 8.12 - Displaying a multiple choice question
JS Bin
var displayQuestion = function (questionAndAnswer) {
var options = [ "A", "B", "C", "D", "E" ];
console.log(questionAndAnswer.question);
questionAndAnswer.answers.forEach(
function (answer, i) {
console.log(options[i] + " - " + answer);
}
);
};
var question1 = {
question : "What is the capital of France?",
answers : [
"Bordeaux",
"F",
"Paris",
"Brussels"
],
correctAnswer : "Paris"
};
displayQuestion(question1);
Listing 8.12 - Displaying a multiple choice question - Tasks 1, 2 & 3
- Add two more question objects.
- Create an array with your question objects.
- Use forEach to call displayQuestion on each of your questions.
var displayQuestion = function (questionAndAnswer) {
var options = [ "A", "B", "C", "D", "E" ];
console.log(questionAndAnswer.question);
questionAndAnswer.answers.forEach(
function (answer, i) {
console.log(options[i] + " - " + answer);
}
);
};
var question1 = {
question : "What is the capital of France?",
answers : [
"Bordeaux",
"F",
"Paris",
"Brussels"
],
correctAnswer : "Paris"
};
// two more questions
var question2 = {
question : "What is the capital of Norway?",
answers : [
"Oslo",
"Stockholm",
"Helsinki",
"N",
"Copenhagen"
],
correctAnswer : "Oslo"
};
var question3 = {
question : "What is the capital of Peru?",
answers : [
"Peru City",
"P",
"Lima"
],
correctAnswer : "Lima"
};
// create an array of questions
var questions = [ question1, question2, question3 ];
// display the questions
questions.forEach(displayQuestion);