A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: [RESOLVED] Array Loop problem

  1. #1
    Junior Member
    Join Date
    May 2011

    resolved [RESOLVED] Array Loop problem


    I am recently working with an application. Where I need 4 unique values from an array in such a way that it should checking random item every time.

    For eg:-

    Main_Array = ["one","two","three","four","five","six","seven "];

    Sub_Array = ["three"]; // initially it holds one value

    for(each value of Sub_Array)
    if(Sub_Array[value] == Main_Array[RandomNumber])
    // It should get another random item from Main_Arr and check
    //next time updated Sub_Arrays values should be checked.

    // I want an array with 3 unique values other then the one already pushed in

    I am trying but I don't get unique values, or something gets wrong which I am not able to solve. Looking forward desperately for some help

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Austin, TX
    Do you need Main_Array to remain unchanged? It doesn't matter much, just determines whether we need to clone it. I'll assume you do. Why put "three" in Main_Array if it's not eligible to be selected?
    * @param n int number of values to select from arr.
    * @param arr Array from which to select values
    * @return a new Array containing n values.
    function selectUniqueValuesFrom(n:int, arr:Array, forbiddenValues:Array):Array{
      if (n > arr.length){
         //throw some error or return null or something.
      var arrClone:Array = arr.filter(notIn(forbiddenValues));
      var toreturn:Array = [];
      for (var i:int = 0; i < n; i++){
        var k:int = Math.floor(Math.random()*arrClone.length);
        var elem:* = arrClone[k];
        arrClone.splice(k, 1);
      return toreturn;
    * Builds a FUNCTION to use as a callback to filter
    * @param values Array of values to exclude via filter
    * @return Function
    function notIn(values:Array):Function{
      return function(item:*, index:int, array:Array):Boolean{
        return values.indexOf(item) == -1;
    Sub_Array = ["three"].concat(selectUniqueValuesFrom(4, Main_Array, ["three"]));
    I got a little fancy with the filter use, but it should work. There are other ways too. The most complicating part was that you want to pre-fill it partially.

  3. #3
    Junior Member
    Join Date
    May 2011

    Thumbs up

    Thanks a lot your code works well. Actually I am making one application where I have exam module. So I coded the correct answer and I push that initially in Sub_array, so I want the rest 3 wrong options from the whole array but unique.

    I just thought that this would be possible way to do this. There are other ways even. But really thankful for help

Tags for this Thread

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