Javascript - while 循环不显示成功选项

Javascript - while loop doesn't show success option

为什么我的代码从不提醒 "be happy"?

它适用于所有其他选项,但不适用于此选项。为什么会这样?

var i = "";
while (i != "YES"){
    if(i == "NO"){
        alert("You should be!");
    }

    else if(i == "YES") {
        alert("Be happy!")
    }

    else{
        if(i == ""){
        }
        else {
            alert("C'mon dude... Answer simply yes or no!");
        }
    }

    i = prompt("Are You happy?").toUpperCase();
}

因为当你进入循环时,条件只是确保i在循环开始时永远不会是'Yes'

将您的 i = prompt("Are You happy?").toUpperCase(); 拉到循环的开头。

When the user is prompted to write down "YES", you exit the while loop. Put it on top

var i = "";
while (i != "YES"){
    i = prompt("Are You happy?").toUpperCase(); //*************** Put it here
    if(i == "NO"){
        alert("You should be!");
    }

    else if(i == "YES") {
        alert("Be happy!")
    }

    else{ //Not needed (do else { alert("com...")} )
        if(i == ""){
        }
        else {
            alert("C'mon dude... Answer simply yes or no!");
        }
    }


}