将多个IF ELSE语句组合到AND语句中会导致脚本停止工作

时间:2017-02-21 01:04:37

标签: javascript if-statement

以下代码正常运行,但another user recommended combining statements使我的代码更高效,更少冗余。我实施了这些更改,认为这不会导致任何问题,但它会使我的整个功能停止。

function loginauth() {
var success = false;
/*roles (privilege levels) are defined as follows:
    priv0   =   User is not logged in
    priv1   =   User is logged in using the account "guest"
    priv2   =   User is logged in using a standard user account
    priv3   =   User is logged in using an elevated user account
    priv4   =   User is logged in using an administrator account
    priv5   =   User is logged in using the super administrator account
*/
var Xusername = document.getElementById("lsr1u").value;
var Xpassword = document.getElementById("lsr1p").value;
if (Xusername == "administrator") {
    if (Xpassword == "5YPwP7$luJailk1b2TCAdSEp7ZCfHUdRfwYm3mwc!1D3BP3ML8^00uoUXIncN8N") {
        success = true;
        role = "priv5";
        userFN = "Administrator";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "guest") {
    if (Xpassword == "guest") {
        success = true;
        role = "priv1";
        userFN = "Guest";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "admin") {
    if (Xpassword == "AdminPassw0rd$0") {
        success = true;
        role = "priv4";
        userFN = "Admin";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else if (Xusername == "jdoe") {
    if (Xpassword == "E2HfYrhyGEwcdWnAVgVD") {
        success = true;
        role = "priv2";
        userFN = "John Doe";
    }
    else {
        document.getElementById("wrong").innerHTML = "The username or password is incorrect."
    }
}
else {
    document.getElementById("wrong").innerHTML = "The username or password is incorrect."
}
if (success) {
    document.getElementById("wrong").innerHTML = ("");
    setTimeout(function() {
        nextauth();
    }, 475);
}
}

简单地将多个if语句组合成单个语句会导致函数停止工作:

function loginauth() {
var success = false;
var Xusername = document.getElementById("lsr1u").value;
var Xpassword = document.getElementById("lsr1p").value;
if (Xusername == "administrator" && Xpassword == "5YPwP7$luJailk1b2TCAdSEp7ZCfHUdRfwYm3mwc!1D3BP3ML8^00uoUXIncN8N") {
    success = true;
    role = "priv5";
    userFN = "Administrator";
}
}
else if (Xusername == "guest" && Xpassword == "guest") {
    success = true;
    role = "priv1";
    userFN = "Guest";
}
else if (Xusername == "admin" && Xpassword == "AdminPassw0rd$0") {
    success = true;
    role = "priv4";
    userFN = "Admin";
}
else if (Xusername == "jdoe" && Xpassword == "E2HfYrhyGEwcdWnAVgVD") {
    success = true;
    role = "priv2";
    userFN = "John Doe";
}
else {
    document.getElementById("wrong").innerHTML = "The username or password is incorrect."
}
if (success) {
    document.getElementById("wrong").innerHTML = ("");
    setTimeout(function() {
        nextauth();
    }, 475);
}
}

绝对 NOTHING 其他内容已被更改。

1 个答案:

答案 0 :(得分:3)

在第一个if语句后你有两个结束括号,这就是在那时关闭你的loginauth方法。

其余代码不会作为该方法调用的一部分进行评估。