Reputation: 25
i am currently makin a identification quiz using as3, i have a codes here that are actuallyworking through buttons, i just want to ask if its possible to convert my code to array statement but ended up with same output? i really like to make my code an array coz its really flexible, thanks guys
var answer:String = answer.text.toLowerCase();
if (answer == "allan joshua mccartney" || answer =="mccartney"
|| answer == "allan" || answer =="joshua" || answer == "joshua mccartney" || answer =="allan joshua" || answer == "allan mccartney") {
score = score +1;
gotoAndStop(2);
as you can see my codes is working but it takes really really long codes to finish a single identifcation quiz bcoz there is so many possible answer, i heard that array is the best way to deal with this bunch of possible answers. thans guys
Upvotes: 1
Views: 44
Reputation: 548
your answer list:
var answersList:Array=new Array("answer1","answer2","answer3");//the list of correct answers
Then:
var answer:String = answer.text.toLowerCase();
if(answersList.indexOf(answer)>-1){//check if the answer is in the answers list
score++;// the short form of "score=score +1;
//the rest of the code
Edit
to check if the answer contains more than one answer:
use a loop to check items in answersList
:
var numAnswers:uint=0;//number of answers found in the answer.
for(var i:uint=0;i<answerslist.length;i++){
if(answer.indexOf(answersList[i])>-1){
numAnswers++;
}
}
numAnswers;//now this variable contains number of answers found in the answer.
of course, it is not so complex and has some problems, for example it answer may contain extra characters that will not be tracked with this code. or some different answers could be part of each other and it won't be tracked. you should do them yourself.
I hope this helps.
Upvotes: 1