JS Switch声明

时间:2015-08-09 00:06:01

标签: javascript switch-statement

我尝试使用两个switch语句来解释来自AJAX调用的一些信息。

代码如下所示: https://jsfiddle.net/8124qnog/2/

基本上,我希望第一个循环检查数组" roleArray"并将值赋给变量" role"。它可以有四个不同的值:

L,S,J或A.

然后,我希望下一个switch语句检查' role'的值。并查看它是L,S,J还是A,并为变量赋值。

例如,当我尝试console.log(aFactor)时,我得到一个未定义的值,这似乎意味着我的循环没有运行或者switch语句没有被正确编码。

我可以这样做吗?

谢谢!

编辑:

代码:

            //ALGORITHM START
            for (i = 0, i < 10; i++;) {
                switch (roleArray[i]) {
                    case "DUO":
                        //assumes top lane duo
                        role = "L";
                        console.log(role);
                        break;
                    case "DUO_CARRY":
                        //assumes bot lane adc
                        role = "A";
                        console.log(role);
                        break;
                    case "DUO_SUPPORT":
                        //assumes bot lane support
                        role = "S";
                        console.log(role);
                        break;
                    case "SOLO":
                        //assumes mid or top solo
                        role = "L";
                        console.log(role);
                        break;
                    case "NONE":
                        //assumes jungle, check lane
                        role = "J";
                        console.log(role);
                        break;
                    default
                        role = "L";
                        break;
                }

            switch (role) {
                case "L":
                    aFactor = 1.00; //kills
                    bFactor = 0.66; //assists
                    cFactor = 0.75; //deaths
                    dFactor = 0.80; //cs
                    eFactor = 0.20; //jungle cs
                    fFactor = 0.55; //counter jungle cs
                    gFactor = 0.50; //cc time
                    break;
                case "A":
                    aFactor = 1.00;
                    bFactor = 0.10;
                    cFactor = 1.00;
                    dFactor = 1.00;
                    eFactor = 0.20;
                    fFactor = 0.30;
                    gFactor = 0.33;
                    break;
                case "S":
                    aFactor = 0.20;
                    bFactor = 2.00;
                    cFactor = 0.33;
                    dFactor = 0.10;
                    eFactor = 0.33;
                    fFactor = 0.55;
                    gFactor = 2.00;
                    break;
                case "J":
                    aFactor = 1.00;
                    bFactor = 1.50;
                    cFactor = 0.75;
                    dFactor = 0.33;
                    eFactor = 1.20;
                    fFactor = 1.60;
                    gFactor = 1.00;
                    break;
            }

2 个答案:

答案 0 :(得分:0)

我试图弄明白,但你的代码没有意义。首先要确保正确关闭你的块。

其次,默认情况下将冒号放入

default: 
// your code

另外,你为什么要这样做?

for (i = 0; i > 10; i++) {
                    var pvpFactor = ((aFactor * killArray[i]) + (bFactor * assistArray[i])) / (cFactor * deathArray[i]);
                    var pveFactor = ((dFactor * csArray[i]) + (eFactor * jungleCSArray[i]) + (fFactor * counterJungleCSArray[i])) / (gameTimeArray[i]);
                    var auxFactor = (gFactor * crowdControlArray[i]);

                    var rawHMC = (0.8 * pvpFactor) + (0.4 * pveFactor) + (2.5 * auxFactor)

                    var HMCarray = [];

无理由地在for循环中声明变量?变量仅对i = 9有效。你为什么要把它放在一个循环中?

答案 1 :(得分:-2)

//语法:

        switch(Expression){
            case 1:
                condition;
                break;
            case 2:
                condition;
                break;
            case 3:
                condition;
                break;
            case 4:
                condition;
                break;
            case 5:
                condition;
                break;
            case 6:
                condition;
                break;
            case 7:
                condition;
                break;
            case 8:
                condition;
                break;
            case 9:
                condition;
                break;
            default:
                last Condition;
        }