首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AS3新手苦苦挣扎

AS3新手苦苦挣扎
EN

Stack Overflow用户
提问于 2017-02-23 15:38:50
回答 1查看 39关注 0票数 0
代码语言:javascript
复制
var animal:String ="Cat";


var isFish:Boolean;

isFish = isItAFish(animal);
trace(isFish);

function isItAFish (animal:String):Boolean {

    var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod");

    for(var i:int = 0; i < fishArray.length; i++){

        if (fishArray[i] == animal){

            return true;

            break;
        }
    }
    return false; 
}

我只是需要帮助来解释这个代码的男孩和女孩。"isFish = isItAFish (动物);trace(isFish);是我混淆的地方。

EN

回答 1

Stack Overflow用户

发布于 2017-02-23 22:22:53

代码语言:javascript
复制
//animal is a string that contains the value "Cat"
var animal:String ="Cat";

//isFish is a boolean that will be used as a flag
var isFish:Boolean;

//isFish value will be changed from the outcome of the function isItAFish with the animal value.
isFish = isItAFish(animal);
trace(isFish);

//This function requires 1 string parameter and returns a boolean.
function isItAFish (animal:String):Boolean
{
     //fishArray is a list of all your possible fishes.
     var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod");

    /*
    We iterate the array to see if animal ("Cat") is inside the fishArray possible values.
    This loop will run exactly the number of times of the array's content. In this case, 4 times.
    */
    for(var i:int = 0; i < fishArray.length; i++)
    {
        /*
        We are comparing the values of the fishArray against animal ("Cat").
        fishArray[i] holds the value of the current loop count.
        For example, the first loop will be fishArray[0] which is "haddock".
        The 4th loop will contain the value "Cod".
        */
        if (fishArray[i] == animal)
        {
            //If we find a match, we return 'true' and stop the loop.
            return true;
            break;
        }   
    }

    //IF the loop ends without any matches we return 'false'.
    return false; 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42409696

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档