A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: as3 compare array elements...cleaner code??

  1. #1
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929

    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!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

  2. #2
    :
    Join Date
    Dec 2002
    Posts
    3,518
    Code:
    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("");
    //
    QuesKeys.map(chkMatch,answerList);
    
    function chkMatch(element:*, index:int, arr:Array):String {
    	trace(tmpAnswerString.match(element.split("?").join(".")));
    	return String(element);
    }

  3. #3
    anyone else hear that? flashpipe1's Avatar
    Join Date
    Jan 2003
    Location
    Upstate NY
    Posts
    1,929
    SWEET!! Very clever...never heard of the map function before, that's excellent...I KNEW there were lots of people out there smarter than me, I was just hoping it might take a little longer for them to turn up.

    Thanks!!
    Love like you've never been hurt, live like there's no tomorrow and dance like nobody's watching.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center