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

时间:2015-07-05 10:10:19

标签: javascript while-loop

为什么我的代码永远不会警告"快乐"?

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

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();
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

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!");
        }
    }


}