Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-21
4
Medium Priority
?
173 Views
Last Modified: 2010-04-03
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?
0
Comment
Question by:alex_wareing
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 20327464
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
0
 
LVL 14

Accepted Solution

by:
tomaugerdotcom earned 2000 total points
ID: 20327471
WHOOPS - I'm missing a bracket. Man it's too early for this.

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

Author Comment

by:alex_wareing
ID: 20327562
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");
}
0
 
LVL 14

Expert Comment

by:tomaugerdotcom
ID: 20327786
Awesome. Glad to have helped!

T
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a very simple example to help those of you who are still migrating from AS2 to AS3 understand the redesigned event model in AS3. In AS2.0, event functions (that is, the function to be performed when an event is fired) were stored as a pro…
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question