或者条件永远不会评估为真
Or condition never evaluates to true
这个 javascript 代码运行成功,但我认为它没有通过它的 "else" 语句,因为它没有打印它的控制台...为什么?
i = 0;
for(var i=1; i<4; i++) {
var crazy = prompt("would you marry me?");
if(crazy === "Yes"|| "yes") {
console.log("hell ya!");
}
/* when it asks "will you marry me" and if the user says either "No" or "no", it does't print "ok..I'' try again next time". instead, it still says "hell ya !" */
else if (crazy ==="No" ||"no") {
console.log("ok..I'll try again next time !");
}
}
var love = false;
do {
console.log("nonetheless, I LOVE YOU !");
}
while(love);
试试这个..虽然是一段不错的代码..这是对某个极客的求婚吗?
i = 0;
for(var i=1; i<4; i++) {
var crazy = prompt("would you marry me?");
if(crazy === "Yes"|| crazy ==="yes") {
console.log("hell ya!");
}
/* when it asks "will you marry me" and if the user says either "No" or "no", it does't print "ok..I'' try again next time". instead, it still says "hell ya !" */
else if (crazy ==="No" ||crazy === "no") {
console.log("ok..I'll try again next time !");
}
}
var love = false;
do {
console.log("nonetheless, I LOVE YOU !");
}
while(love);
尝试这样的事情,
if(crazy.toUpperCase() === "YES") {
console.log("hell ya!");
}
解释如下:
crazy === "Yes" || "yes"
这条语句做了以下事情:
- 比较
crazy
是否与字符串"Yes"
相同
- 如果不相同,“使用”
"yes"
将其放入 if
语句中,您将得到:
- 执行
if
块中的所有内容,
- …如果
crazy
等同于"Yes"
,或
- …如果
"yes"
.
"yes"
在 if
语句中本身意味着什么?它被评估为true
.
每个不是空字符串的字符串都被评估为 true
,每个空字符串都被评估为 false
。请参阅 Boolean
and falsy values.
上的 MDN 文章
您可以通过在控制台中输入像这样的双重否定表达式来验证这一点:
!!"yes"; // true
!!"test"; // true
!!""; // false
你需要做的是第二次比较:
if(crazy === "Yes" || crazy === "yes")
和
else if(crazy === "No" || crazy === "no")
替代方法包括 crazy.toLowerCase() === "yes"
仅用于一次比较,或 ["Yes", "yes"].includes(crazy)
。
这个 javascript 代码运行成功,但我认为它没有通过它的 "else" 语句,因为它没有打印它的控制台...为什么?
i = 0;
for(var i=1; i<4; i++) {
var crazy = prompt("would you marry me?");
if(crazy === "Yes"|| "yes") {
console.log("hell ya!");
}
/* when it asks "will you marry me" and if the user says either "No" or "no", it does't print "ok..I'' try again next time". instead, it still says "hell ya !" */
else if (crazy ==="No" ||"no") {
console.log("ok..I'll try again next time !");
}
}
var love = false;
do {
console.log("nonetheless, I LOVE YOU !");
}
while(love);
试试这个..虽然是一段不错的代码..这是对某个极客的求婚吗?
i = 0;
for(var i=1; i<4; i++) {
var crazy = prompt("would you marry me?");
if(crazy === "Yes"|| crazy ==="yes") {
console.log("hell ya!");
}
/* when it asks "will you marry me" and if the user says either "No" or "no", it does't print "ok..I'' try again next time". instead, it still says "hell ya !" */
else if (crazy ==="No" ||crazy === "no") {
console.log("ok..I'll try again next time !");
}
}
var love = false;
do {
console.log("nonetheless, I LOVE YOU !");
}
while(love);
尝试这样的事情,
if(crazy.toUpperCase() === "YES") {
console.log("hell ya!");
}
解释如下:
crazy === "Yes" || "yes"
这条语句做了以下事情:
- 比较
crazy
是否与字符串"Yes"
相同
- 如果不相同,“使用”
"yes"
将其放入 if
语句中,您将得到:
- 执行
if
块中的所有内容,- …如果
crazy
等同于"Yes"
,或 - …如果
"yes"
.
- …如果
"yes"
在 if
语句中本身意味着什么?它被评估为true
.
每个不是空字符串的字符串都被评估为 true
,每个空字符串都被评估为 false
。请参阅 Boolean
and falsy values.
您可以通过在控制台中输入像这样的双重否定表达式来验证这一点:
!!"yes"; // true
!!"test"; // true
!!""; // false
你需要做的是第二次比较:
if(crazy === "Yes" || crazy === "yes")
和
else if(crazy === "No" || crazy === "no")
替代方法包括 crazy.toLowerCase() === "yes"
仅用于一次比较,或 ["Yes", "yes"].includes(crazy)
。