Link to home
Start Free TrialLog in
Avatar of alex_wareing
alex_wareing

asked on

Dynamically creating a CASE/IF statment based on the length of an array

I am creating a quiz based flash game. All the questions are stored in an array, same with the answers. For each question a function is run which loads a movieclip and inserts the question and answer. You can see this in the code below. The _global.questionCounter is simply a variable that counts which question the code is at. This is in frame 1 and 2 of a movie which loops based on the answer to the question.

//Questions
var questions = new Array();
questions[0] = "Here is the text for question 1";
questions[1] = "Here is the text for question 2";
questions[2] = "Here is the text for question 3";
questions[3] = "Here is the text for question 4";
questions[4] = "Here is the text for question 5";

//Answers
var answers = new Array();
answers[0] = "Blue";
answers[1] = "Red";
answers[2] = "Green";
answers[3] = "Yellow";
answers[4] = "Pink";


switch (_global.questionCounter) {
      case 0 :
        loadBox(answers[0], questions[0]);
        break;
      case 1 :
        loadBox(answers[1], questions[1]);
        break;
      case 2 :
        loadBox(answers[2], questions[2]);
        break;
      case 3 :
        loadBox(answers[3], questions[3]);
        break;
      default :
        trace("Done");
        break;
}

However this requires manual writing of the case statment. I want to know if there is someway of reproducing the same effect but automatically producing it from the array. ie: counting the number of records and the writing the relevant case statement. Is there someway of doing this?
Avatar of tomaugerdotcom
tomaugerdotcom
Flag of Canada image

Well, I'm not sure about creating the case statement dynamically, but you're absolutely right that your code as it stands is very labour intensive. Let me see if I understand correctly:

based on the _global.questionCounter variable (which will be an integer) you want to loadBox with the appropriate question and answer from the array, right?

So, this can (and really, really should) be written not as a case statement, but just by using the _global.questionCounter variable as your index to both arrays:

// --- replaces the entire switch block:
loadBox(answers[_global.questionCounter], questions[_global.questionCounter];
// -- end

Beautiful in its simplicity, eh? Instead of using a numerical literal (1, 2, 3 etc) in the function call as your array index eg: answers[1], just use the variable directly eg: answers[_global.questionCounter]

Let me know how this turns out.

Tom
ASKER CERTIFIED SOLUTION
Avatar of tomaugerdotcom
tomaugerdotcom
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of alex_wareing
alex_wareing

ASKER

Great works perfectly, i wrapped it with an IF statement to enable the number of questions to be based on the array:

if (_global.questionCounter < questions.length) {
      loadBox(answers[_global.questionCounter], questions[_global.questionCounter],_global.questionCounter+1);
} else {
      trace("Done");
}
Awesome. Glad to have helped!

T