2

I am writing a javascript program to dynamically print the elements of a multidimensional array to create a quiz. My structure is correct but I'm trying to print the 'choice' elements so they are displayed as radio buttons for the choices of each question.

Javascript:

var dataquestions = [];

dataquestions[0] = {question: "Who is the prime Minister of the United Kingdom?",     choice0: "David Cameron", choice1: "Tony Blair", choice2:"Gordon Brown", answer:"David Cameron"};

 dataquestions[1] = {question:"In what year was the Declaration of Independence signed?", choice0:"1985", choice1:"1492", choice2:"1776", answer:"1776"};

dataquestions[2] = {question:"Which country is in Europe?", choice0:"Pakistan", choice1:"France", choice2:"Australia", answer:"France"};

for (var i=0; i<=2; i++)
{
document.getElementById('quizcontain').innerHTML+= '<p>'+ dataquestions[i].question+'</p><br><br>';

for (var j=0; j<=2; j++){
document.getElementById('quizcontain').innerHTML+='<input type="radio" name='+j+'>'+dataquestions[i].choice+j+'<br>';
    }
}

Output:

Who is the prime Minister of the United Kingdom?

undefined
undefined
undefined
In what year was the Declaration of Independence signed?

undefined
undefined
undefined
Which country is in Europe?

undefined
undefined
undefined

3 Answers 3

1

You cannot use

dataquestions[i].choice+j

to access the properties of the objects. But, you can use the subscript notation, like this

dataquestions[i]["choice" + j]
Sign up to request clarification or add additional context in comments.

4 Comments

@JeffP. You are welcome :) Please forget not to tick this answer :)
Nice , @thefourtheye.
If this answers your question you are supposed to accept. As future reference for other users @jeff-p
@NullSoulException I think OP is aware of that :)
1

This part of your code:

dataquestions[i].choice+j

Gets evaluated as the concatenation of dataquestions[i].choice and j, which is effectively undefined + j. To reference a dynamically named property you have to concatenate 'choice' and j and then use [] dereferencsing, so dataquestions[i]['choice' + j].

Secondly, it would be a good thing to normalize your data structure:

var dataquestions = [{
    question: "Who is the prime Minister of the United Kingdom?",
    choices: ["David Cameron", "Tony Blair", "Gordon Brown"],
    answer: 0
}, {
    question: "In what year was the Declaration of Independence signed?",
    choices: ["1985", "1492", "1776"],
    answer: 2
}, {
    question:"Which country is in Europe?", 
    choices: ["Pakistan", "France", "Australia"],
    answer: 1
}];

Then:

var container = document.getElementById('quizcontain');
for (var i=0; i < dataquestions.length; i++) {
    var question = dataquestions[i],
    html;

    html = '<p>' + question.question + '</p><br><br>';

    for (var j = 0; j < question.choices.length; ++j) {
        html += '<input type="radio" name="' + j + '">' + question.choices[j] + '<br>';
    }

    container.innerHTML += html;
}

Demo

2 Comments

That's neat @jack. It have been a while since I used JavaScript but looking at theses answers refreshes my skills
This is a great structure but it is only printing out the last question.
0

use

dataquestions[i]["choice" + j]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.