views:

503

answers:

2

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?)

A: 

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);
                }
            }

        }
    }
redconservatory
A: 

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.
Amarghosh