as3 compare array elements...cleaner code??
I've got a program where a user answers a series of yes/no questions (tracked as 0/1) and ends up with an array, answerList, that looks like [0,1,1,0,0]
I've got another array (QuesKeys) that has a variety of combinations [01100, 11001, 00011, 0??110] that I need to match the answerList to.
The tricky part, is that some of the combinations aren't dependent on certain answers, hence the "?" in the last element. I'm trying to figure out a nice clean way to test the answerList "chunk" against the elements in the QuesKeys.
Here's what I've got, it works, I'm just wondering if there's a more "elegant" way to do this...
Code:
//test arrays for debugging
var answerList:Array=new Array(0,1,1,0,0);
var QuesKeys:Array=new Array("01000","00110","01101","0??00","1???0","01100");
//convert the array to a string with no commas
var tmpAnswerString:String=answerList.toString();
tmpAnswerString=tmpAnswerString.split(",").join("");
trace(tmpAnswerString);
//break answer string into array
var tmpAnsArray:Array=tmpAnswerString.split("");
var tmpQuesArray:Array = new Array();
var matchedConfig:Number;
//break each array element from QuesKeys into a new array
for (var i = 0; i < QuesKeys.length; i++) {
tmpQuesArray=QuesKeys[i].split("");
}
//match each element of the array, excluding ?
var matched:Boolean=false;
for (var k = 0; k < QuesKeys.length; k++) {
var matchedTotal:Number=0;
tmpQuesArray=QuesKeys[k].split("");
for (var j = 0; j < tmpAnsArray.length; j++) {
if (tmpQuesArray[j]=="?") {
matchedTotal++;
} else {
if (tmpAnsArray[j]==tmpQuesArray[j]) {
matchedTotal++;
}
}
if (matchedTotal==5) {
matched=true;
break;
}
}
if(matched){
break;
}
}
Thanks!!