为什么我的代码永远不会警告"快乐"?
它适用于其他所有选项,但不适用于此选项。为什么这样?
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();
}
答案 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!");
}
}
}