【发布时间】:2016-01-24 13:12:00
【问题描述】:
我想比较用户在提示符下引入的两个字符串。
如果我这样做 (bar == "是" || bar == "否")
它可以正常工作,但如果我使用 !==
(bar !== "yes" || bar !== "no")
程序总是返回
alert("第二个值必须是\'yes\'或\'no\'");
即使我将“是”或“否”作为第二个值。
do{
var values = window.prompt("Introduce a title, and \'yes\' o \'no\' separted with a comma.");
var longValues = values.length;
var comma = values.indexOf(",");
var title = values.substr(0,comma);
var bar = values.substr(comma+1,longValues);
var longTitle = title.length;
if (longTitle <3 || longTitle >30){
alert("The title must be greater than 3 characters and less than 30 characters");
}
else if (bar !== "yes" || bar !== "no"){
alert("The second value must be \'yes\' or \'no\'");
}
else{
alert("Correct!");
}
}while((longTitle <3 || longTitle >30) && (bar !=="yes" || bar !=="no"))
【问题讨论】:
-
从逻辑上讲,这是完全正确的。任何一个 - not "yes' 或 not "no" - 总是正确的(并且可以选择两者)。
标签: javascript string string-comparison comparison-operators