redconservatory
redconservatory

Reputation: 21924

Actionscript 3: Check an array for a match

If you have an array with six numbers, say:

public var check:Array = new Array[10,12,5,11,9,4];

or

public var check:Array = new Array[10,10,5,11,9,4];

How do you check for a match (of a pair?)

Upvotes: 4

Views: 10332

Answers (2)

Amarghosh
Amarghosh

Reputation: 59451

Array class has an indexOf method:

function indexOf(searchElement:*, fromIndex:int = 0):int

Searches for an item in an array by using strict equality (===) and returns the index position of the item.

Parameters

  • searchElement:* — The item to find in the array.
  • fromIndex:int (default = 0) — The location in the array from which to start searching for the item.

Returns

  • int — A zero-based index position of the item in the array. If the searchElement argument is not found, the return value is -1.

Upvotes: 6

redconservatory
redconservatory

Reputation: 21924

Got it (I think). Used the following:

public var match:Array = [10,12,5,10,9,4];

   checkArray(match);

   private function checkArray(check:Array) {

    var i:int;
    var j:int;

    for (i= 0; i < check.length; i++) {
        for (j= i+1; j < check.length; j++) {
            if (check[i] === check[j]) {
                trace(check[i] + " at " + i + " is a match with "+check[j] + " at " + j);
                }
            }

        }
    }

Upvotes: 0

Related Questions