[英]Multiple if-else isn't working
var x = prompt("number between 50 and 100");
if (x.match(/hi/)) {
alert("cool");
} else {
alert("neni cool");
} else if (x==50 || x==100) {
alert("skevele");
} else {
alert("nic");
}
Can anyone explain why else if isn't working? 谁能解释为什么如果不起作用呢? Thanks for answers.
感谢您的回答。
You can't stack them like that. 您不能像这样堆叠它们。
}else{
statements are only ever the last cases in an if/then/else statement, because it only does it if the other cases haven't. }else{
语句仅是if / then / else语句中的最后一种情况,因为只有在其他情况没有时才这样做。
if(condition){
what to do
}else if(condition 2){
what to do if the first condition was not met
}else{
if all else fails
}
You are putting an else if
after an else
. else if
在else
之后,你就在else if
else
。 The else if
will not be evaluated correctly, because else
ends the if
statement. else if
将无法正确评估,因为else
结束了if
语句。 If you want both statements to work, then try: 如果您希望这两个语句都起作用,请尝试:
f (x.match(/hi/)) {
alert("cool");
} else {
alert("neni cool");
}
if (x==50 || x==100) {
alert("skevele");
} else {
alert("nic");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.