Get Programming with JavaScript - Listing 8.12

Listing 8.12 - Displaying a multiple choice question

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);

Further Adventures

Listing 8.12 - Displaying a multiple choice question - Tasks 1, 2 & 3

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);