function that matches the selected string from an array. A good utility function to have.


hello everyone,

 

i wrote small function loops through array (in case, lets call fullarray), , matches chosen string copies array called "yesarray", , copies unmatched elements of 'fullarray' third array called "noarray". pretty original array used reference.

 

function works expected, however, wonder if have taken long way write it? please share thoughts on it. lot.

 

var fullarray:array = ["one","two","three","four","five","six","seven","eight"];
var yesarray:array = [];
var noarray:array = [];
var ans:string = "three";

 

checkanswer(ans);

 

function checkanswer(s:string):void
{
    var ele:int = (fullarray.indexof(s));
    var isfound:boolean = (fullarray.indexof(s))? true : false;
    if(isfound)
    {
        if(yesarray.length > 0)
         {
             for(var i:int=0; i<yesarray.length;i++)
             {
                yesarray.splice(yesarray[i]);
                yesarray.push(fullarray[ele]);
             }
             for(var j:int=0; j<noarray.length; j++)
             {
                 noarray.splice(noarray[j]);
             }
             for(var k:int=0; k<fullarray.length; k++)
             {
                 noarray.push(fullarray[k]);
                 //trace(noarray);
             }
         }
         else
         {
            yesarray.push(fullarray[ele]);
            trace(yesarray);
            for(var m:int = 0; m < fullarray.length; m++)
            {
                noarray.push(fullarray[m]);
            }
            noarray.splice(noarray.indexof(ans),1);
           // trace(noarray);
         }
    }
}  

:

 

yesarray=fullarray.slice();

noarray = [ans];


checkanswer(ans,yesarray);

 

function checkanswer(s:string,a:array){
    for(var i:uint=a.length-1;i>=0;i--){
        if(s==a[i]){
            a.splice(i,1);
        }
    }
}

 

///////////////////////////////////////////

 

after calling checkanswer, noarray contains matching string , yesarray contains unmatched elements of fullarray.  there's no provision noarray 0 length because doesn't appear allow possibilty of no matches.



More discussions in ActionScript 3


adobe

Comments

Popular posts from this blog

localhost/joomla15/administrator doesnt work - Joomla! Forum - community, help and support

Using Arduino Uno R3 to bootload the atmega328-PU (non pico)

cf_sql_integer vs cf_sql_bigint vs cf_sql_int??