Below is a comparison between a two-dimensional array code:
var questions = [
['How many states are in the United States?', 50],
['How many continents are there?', 7],
['How many legs does an insect have?', 6]
];
and its converted version to an array object:
var questions = [
{ question: 'How many states are in the United States?', answer: 50 },
{ question: 'How many continents are there?', answer: 7 },
{ question: 'How many legs does an insect have?', answer: 6 }
];
Both versions have corresponding for loops.
for (var i = 0; i < questions.length; i += 1) {
question = questions[i][0];
answer = questions[i][1];
response = prompt(question);
response = parseInt(response);
if (response === answer) {
correctAnswers += 1;
correct.push(question);
} else {
wrong.push(question);
}
}
and
for (var i = 0; i < questions.length; i += 1) {
question = questions[i].question;
answer = questions[i].answer;
response = prompt(question);
response = parseInt(response);
if (response === answer) {
correctAnswers += 1;
}
}
What sets apart two-dimensional arrays from array objects? Could the way data is stored impact the efficiency of running a for loop? How can one determine which storage method is better to use?