1

I've looked everywhere, and although there are many explanations, I can't seem to wrap my head around it.

Here is my xml structure:

 <question> 
            <q1> Who coined the term “Clinical Psychology”? </q1>       
            <answer> Lightner Witmer </answer>

            <option1> Stanley Hall </option1>
            <option2> Lightner Witmer </option2>
            <option3> Henry P. David </option3>
    </question>

I can loop through fine and pick out the questions and answers, then throw them into separate arrays. The problem I'm having is looping and pulling the options into a multidimensional array like such:

var one:Array = new Array( 3 ); 
one[0] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[1] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];
one[2] = ["Stanley Hall", "Lightner Witmer", "Henry P. David"];

Any help would be greatly appreciated.

2 Answers 2

2
    var answers:Array = [];

    for(var i = 0; i< xml.question.length; i++){
    var node:Array = [];
    node.push(xml.question[i].option1);
    node.push(xml.question[i].option2);
    node.push(xml.question[i].option3);

//or
    var node:Array = [xml.question[i].option1,xml.question[i].option2,xml.question[i].option3];

    answers.push(node);

    }

answers for questions can now be retrieved:

answers[questionIndex][answerIndex];

Hope that helps

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure you are setting up the array in the form you want but, going off the code you posted.
Have you tried?

var one:Array = new Array( 3 ); 
one[0] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[1] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );
one[2] = new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );



With something like this I would prefer to have a questions array with objects as the elements. Each object would hold all the info about that question. This would allow for a custom class to be assigned for each question. Or a basic dynamic object could be used

var questions:Array = new Array();

var obj:Object = new Object();
obj.question = "Who coined the term 'Clinical Psychology'?"
obj.correctAnswer = "Lightner Witmer";
obj.possibleAnswers =new Array( "Stanley Hall", "Lightner Witmer", "Henry P. David" );

questions.push( obj );

2 Comments

This way does seem to make more sense. If I have time tonight, I'll try it out. Thank you!
Yeah, its a little more coding but a lot more easier to read. unlike my writing skills.

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.