0

I have a bunch of arrays (Q001, Q002, Q003...). Each array is a question plus 4 possible answers. I want to randomly choose which question to display. So I randomly assign an array name to the variable chosenQuestion. Then I try to write the first element of that array into a text box, like so: txt.Question.text = [chosenQuestion][0];

For example, if the the chosen question is Q001 then it puts Q001 in the text box. But I really want in the text box is the first element of the array Q001.

1 Answer 1

1

Supposed that you have your 3 arrays of questions, so to get the question, you can do like this :

var question_01:Array = ['question 01', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];
var question_02:Array = ['question 02', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];
var question_03:Array = ['question 03', 'answer 01', 'answer 02', 'answer 03', 'answer 04'];

var selected_question:int = Math.ceil(Math.random() * 3);   // gives : 1, 2 or 3

trace(this['question_0' + selected_question][0]);   // gives : question 01, for example

You can also put your questions arrays into an array like this :

var questions:Array = [
    ['question 01', 'answer 011', 'answer 02', 'answer 03', 'answer 04'],
    ['question 02', 'answer 012', 'answer 02', 'answer 03', 'answer 04'],
    ['question 03', 'answer 013', 'answer 02', 'answer 03', 'answer 04']
];
selected_question = Math.floor(Math.random() * 3);  // gives : 0, 1 or 2

trace(questions[selected_question][0]); // gives : question 02, for example

Hope that can help.

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

1 Comment

Thank you akomozo. You are AWESOME! I was missing the "this". The multidimensional array is a great solution too. Now I have two options.

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.