Javascript 避免多个嵌套的if语句

Javascript 避免多个嵌套的if语句,javascript,if-statement,nested,code-cleanup,Javascript,If Statement,Nested,Code Cleanup,我正在编写一个函数,根据4个变量的状态将特定(预定义)变量推送到预定义的数组中,所有变量的范围都在1到5之间(它们是网页上单选按钮按下的结果) 如果变量等于A、B、C和D,则预定义数组X、Y和Z的预定义变量定义如下: X=A和B的组合 Y=C和D的组合 Z=A、B、C和D的组合 下面是我使用嵌套的if/else语句实现上述问题的解决方案(a、B、C和D是动机、方向、影响和相关性)。然而,我发现这个解决方案并不雅观。我都想写更干净的代码,让同事们更容易阅读。用什么最优雅的方式打印出来?我应该使用函

我正在编写一个函数,根据4个变量的状态将特定(预定义)变量推送到预定义的数组中,所有变量的范围都在1到5之间(它们是网页上单选按钮按下的结果)

如果变量等于A、B、C和D,则预定义数组X、Y和Z的预定义变量定义如下:

X=A和B的组合

Y=C和D的组合

Z=A、B、C和D的组合

下面是我使用嵌套的if/else语句实现上述问题的解决方案(a、B、C和D是动机、方向、影响和相关性)。然而,我发现这个解决方案并不雅观。我都想写更干净的代码,让同事们更容易阅读。用什么最优雅的方式打印出来?我应该使用函数、开关语句或其他什么吗

以下是整个功能:

function getRadioParameters (influence, relevance, motivation, orientation) {
    if (influence >= 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(highInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(highInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }
    else if (influence < 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(lowInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(lowInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }

}
功能参数(影响、相关性、动机、方向){
如果(影响>=3){
如果(相关性>=3){
影响相关性推送(高影响高相关性);
如果(动机>=3){
如果(方向>=3){
动机导向(highMotivationHighOrientation);
stkimagesar.push(getImage('hhh');
}
否则如果(方向<3){
动机导向或推动(高动机低导向);
stkimagesar.push(getImage('HHHL');
}
否则{
动机定向推送(“”);
标准压力(“”);
console.log('方向有问题。它是=',方向)
}
}
否则如果(动机<3){
如果(方向>=3){
动机定向(低动机高定向);
stkimagesar.push(getImage('HHLH');
}
否则,如果(方向=3){
如果(方向>=3){
动机导向(highMotivationHighOrientation);
stkimagesar.push(getImage('HLHH');
}
否则如果(方向<3){
动机导向或推动(高动机低导向);
stkimagesar.push(getImage('hlhlhl');
}
否则{
动机定向推送(“”);
标准压力(“”);
console.log('方向有问题。它是=',方向)
}
}
否则如果(动机<3){
如果(方向>=3){
动机定向(低动机高定向);
stkimagesar.push(getImage('HLLH');
}
否则,如果(方向=3){
影响相关性推送(低影响高相关性);
如果(动机>=3){
如果(方向>=3){
动机导向(highMotivationHighOrientation);
stkimagesar.push(getImage('LHHH'));
}
否则如果(方向<3){
动机导向或推动(高动机低导向);
stkimagesar.push(getImage('lhl');
}
否则{
动机定向推送(“”);
标准压力(“”);
console.log('方向有问题。它是=',方向)
}
}
否则如果(动机<3){
如果(方向>=3){
动机定向(低动机高定向);
stkimagesar.push(getImage('LHLH');
}
否则,如果(方向=3){
如果(方向>=3){
动机导向(highMotivationHighOrientation);
stkimagesar.push(getImage('LLHH'));
}
否则如果(方向<3){
动机导向或推动(高动机低导向);
stkimagesar.push(getImage('LLHL');
}
否则{
动机定向推送(“”);
标准压力(“”);
console.log('方向有问题。它是=',方向)
}
}
否则如果(动机<3){
如果(方向>=3){
动机定向(低动机高定向);
stkimagesar.push(getImage('LLLH');
}

else if(orientation以下是使用函数将其抽象出来的示例:

function getRadioParameters (influence, relevance, motivation, orientation) {
    if (influence >= 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(highInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(highInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('HLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('HLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }
    else if (influence < 3) {
        if (relevance >= 3) {
            influenceRelevanceArr.push(lowInfluenceHighRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LHLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LHLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else if (relevance < 3) {
            influenceRelevanceArr.push(lowInfluenceLowRelevance);
            if (motivation >= 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(highMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLHH'));
                }
                else if (orientation < 3) {
                    motivationOrientationArr.push(highMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLHL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation)
                }
            }
            else if (motivation < 3) {
                if (orientation >= 3) {
                    motivationOrientationArr.push(lowMotivationHighOrientation);
                    stkImagesArr.push(getImage('LLLH'));
                }
                else if (orientation<3) {
                    motivationOrientationArr.push(lowMotivationLowOrientation);
                    stkImagesArr.push(getImage('LLLL'));
                }
                else {
                    motivationOrientationArr.push('');
                    stkImagesArr.push('');
                    console.log('problem with orientation. It is = ', orientation);
                }
            }
            else {
                motivationOrientationArr.push('');
                stkImagesArr.push('');
                console.log('problem with motivation. It is = ', motivation);
            }

        }
        else {
            influenceRelevanceArr.push('');
            motivationOrientationArr.push('');
            stkImagesArr.push('');
            console.log('problem with relevance. It is =', relevance);
        }
    }

}
//X=A和B的组合。
//Y=C和D的组合。
//Z=A、B、C和D的组合。
//在本例中:
//A>5,B>10,C>15,D>20
功能返回特性(输入){
如果(输入[0]>5&&input[1]>10&&input[2]>15&&input[3]>20){
返回'Z';
}
如果(输入[2]>15和输入[3]>20){
返回“Y”;
}
如果(输入[0]>5和输入[1]>10){
返回'X';
}
返回“未满足标准”;
}
设test1=[10,5,33,5];
设test2=[10,15,32,50];
设test3=[20,20,10,9];
设test4=[0,5,50,50];
log(returnCharacteristic(test1));
log(returnCharacteristic(test2));
日志(returnCharacteristic(test3));

console.log(returnCharacteristic(test4));
单独获取每个单独的值可能更容易,然后以某种方式组合它们-这一切都取决于您的数据结构。从我目前看到的情况来看,我认为您可以这样做(这是一个高级别的操作,您必须填写完整的de)