Java Reference
In-Depth Information
Quiz Ninja Project
Now it's time to take another look at our Quiz Ninja project. We're going to use an object to
store the information about the quiz. Open up scripts.js in the js folder and enter the follow-
ing at the top of the file:
scripts.js
(excerpt)
quiz = {
"name":"Super Hero Name Quiz",
"description":"How many super heroes can you name?",
"question":"What is the real name of ",
"questions": [
{ "question": "Superman", "answer": "Clarke Kent" },
{ "question": "Batman", "answer": "Bruce Wayne" },
{ "question": "Wonder Woman", "answer": "Dianna Prince" }
]
}
This is an object that contains information about the quiz in its properties. For example there
are
name
and
description
properties that contain string values about the quiz. There
is also a
questions
property that contains an array of objects. These objects replace the
nested arrays we used in the previous chapters and have properties of
question
and
an-
swer
. This means that we can refer to a question as
quiz.questions[i].question
instead of
quiz[i][0]
and an answer as
quiz.questions[i].answer
instead of
quiz[i][1]
. So we also now need to make a change to the
for
loop in our code when
we select the question:
scripts.js
(excerpt)
for(var i=0, question, answer, max=quiz.questions.length;
i<max;
↵
i++) {
question = quiz.questions[i].question; // change is made
here
answer = ask(question);
check(answer);
}
