Copy link to clipboard
Copied
Hello. I am trying to create a multiple choice quiz using an array for the questions and answers data. Each array has 4 answer options which I have put the correct answer as the first option. I would like to randomize each question's answers so the correct answer isn't always answer A. Here is some of the code. Thanks for you time
var testModel: Array = [{
num: "30",
q: “what color is red?",
a1: " red ",
a2: " blue ",
a3: " green ",
a4: " pink"
}];
a_btn.addEventListener(MouseEvent.CLICK, goclick_a);
b_btn.addEventListener(MouseEvent.CLICK, goclick_b);
c_btn.addEventListener(MouseEvent.CLICK, goclick_c);
d_btn.addEventListener(MouseEvent.CLICK, goclick_d);
var user_Test_ans: Array = new Array();
var newtestModel: Array = shuffle1Array(testModel);
var index1: int = 0;
var userTanswer: String = "";
questions_txt.text = newtestModel[index1]["q"];
a1_txt.text = newtestModel[index1]["a1"];
a2_txt.text = newtestModel[index1]["a2"];
a3_txt.text = newtestModel[index1]["a3"];
a4_txt.text = newtestModel[index1]["a4”];
Copy link to clipboard
Copied
You might be confusing the Array with an Object. Your Array as shown only holds one item, an Object that consists of a question and 4 answers and what I will guess is the value of the question. Where you shuffle the array you will not be doing anything to shuffle the answers within each question, you will only be shuffle the objects, essentially randomizing the order in which the questions get asked (assuming your shuffle1Array does what it would seem to).
If you want to randomize the answers you will need to place them in an array as well and then shuffle that before displaying the asnwer selections. You can probably do this without changing the Object so that you can refer to the "a1" property of the object to determine if a correct answer is selected.
Copy link to clipboard
Copied
You could do it without putting the answers in an array like this (but it's silly):
var answerLocations:Array = ['a1', 'a2', 'a3', 'a4'];
var whereToGetAnswers:Array = [];
while (answerLocations.length>0) {
var index:int = Math.random() * answerLocations.length -1;
whereToGetAnswers.push(answerLocations.splice(index, 1));
}
a1_text.text = newTextModel[yourOriginalIndex][whereToGetAnswers[0]];
//etc.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now