PDA

View Full Version : loops, arrays, objects


Sarah012
05-11-2004, 10:30 AM
I have a XML document that contains a series of questions, attributes, images and answers. I am trying to construct an ActionScript object for each Question which contains the questions attributes, the image name, and an all of the answers. I have set up an array which loops through the XML and traces out each piece of information. Here is the code:


function buildQuestionArray() {
QuestionsArray = new Array ()
for (var i=0; i<TestNode.childNodes.length; i++){
curQuestion=TestNode.childNodes[i];
var AnswersArray = new Array();
for (var j=0; j<curQuestion.childNodes.length; j++){
curParts=curQuestion.childNodes[j];
for (var b=0; b<curParts.childNodes.length; b++){
if (curParts.childNodes[b].nodeName==null){
curImage=curParts.childNodes[b];
}else if (curParts.childNodes[b].nodeName!=null){
curImage=null;
curAnswers=curParts.childNodes[b];
}
AnswersArray[b]= curAnswers.childNodes
}
}
QuestionsArray[i] = new Question (curQuestion.attributes.number, curQuestion.attributes.format, curQuestion.attributes.correctanswer, curQuestion.attributes.text, curImage, AnswersArray)
}
makeQuestion (currentQuestion);
}

function Question (numbers, format, correct, Text, Image, Answers) {
this.numbers=numbers
this.correct=correct
this.Text=Text
this.format=format
this.Image=Image
trace (correct);
trace (Text);
trace (format);
trace (Image);
trace (Answers);
}

What I was hoping I would get for output was:

False
The capital of Connecticut is Bloomfield
1
null
True,False
A
What is the capital of Connecticut?
2
CTMap.jpg
Hartford,Bloomfield,New Haven,Washington D.C.

What I am getting:

False
The capital of Connecticut is Bloomfield
1
null
True,False
A
What is the capital of Connecticut?
2
CTMap.jpg
Washington D.C.,Bloomfield,New Haven,Washington D.C.

(notice the first answer of the second question should be Hartford)
Also, the whole thing traces out four times instead on once.

Any suggestions?