0

I'm creating a program in Flash using Actionscript 3. I was woundering if I could write an «if-statement» that checks if the text the user inputs is already in an array.

Like, if you have an array:

var alphabet:Array = new Array("a","b","c","d","e")

I want to make a statement like

If ('a' exist in alphabet)

Is there any way I can do that?

1
  • It would be nice if you close the question somehow, because it's important to not leave unanswered questions - people keep looking at them as they think they can help. Commented Jun 3, 2014 at 8:29

2 Answers 2

4

You can use indexOf to find if the element is in the Array:

if (alphabet.indexOf('c') != -1) {
    // the element is there
}

indexOf actually returns the position at which the element is found, and -1 if the element is missing. So this is a simple check.

Sign up to request clarification or add additional context in comments.

Comments

0

If your array is filled with random entries, you should parse the array one by one.

You can create a function which you supply the parameters of the array and the string you search for like this one.

static public function checkIfExists(txt:String, array:Array):Boolean 
{
    for (var i:int = 0; i < array.length; i++) 
    {
        if (array[i] == txt) 
        {
            return true;
        }
    }
    return false;
}

Then you can call it by

checkIfExists('a', alphabet);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.