find the matching items given in an array

actionscript-3

Solution

Simply, you can do this:

private function getCount(fruitArray:Array, fruitName:String):int {
    var count:int=0;
    for (var i:int=0; i<fruitArray.length; i++) {
        if(fruitArray[i].toLowerCase()==fruitName.toLowerCase()) {
            count++;
        }
    }
    return count;
}

var fruit:Array = ["apples", "oranges", "grapes", "oranges", "apples", "grapes"];
var appleCount=getCount(fruit, "apples"); //returns 2
var grapeCount=getCount(fruit, "grapes"); //returns 2
var orangeCount=getCount(fruit, "oranges"); //returns 2

Problem

i don't understand arrays in functions, but how do you find the matching items given in an array? for example: ``` var fruit:Array = ["apples", "oranges", "grapes", "oranges", "apples", "grapes"]; ``` how can i get it to show only the number of apples in the array?

Original source