简体   繁体   English

为什么不会“||” (或)在我的 javascript function 中工作?

[英]why wont “||” (or) work in my javascript function?

I am trying to write the code so that if if given 2 numbers that represent my birthday (month = 11, day = 3) it would log "how did you know".我正在尝试编写代码,以便如果给定 2 个代表我的生日(月 = 11,日 = 3)的数字,它将记录“你怎么知道”。 anything else would return "Just Another Day".其他任何东西都会返回“又是一天”。 Also need the order of month and day to not matter.也需要月份和日期的顺序无关紧要。

I have written it as:我把它写成:

function birthday (month,day){
  let result;
  if( month == "11" && day == "3") || (day == "3" && month = "11"){
      result = "How did you know?";
     }
      else {
       result = "Just Another Day";
      }
  return result;
}

console.log(birthday(3, 11));

but it returns and tell me the ||但它返回并告诉我|| (or) is an unexpected token...How else could I write this? (或)是一个意外的标记......我还能怎么写这个?

You have two conditions on the two sides of the || ||的两侧有两个条件operator that say the exact same thing.操作员说完全相同的话。 On the second condition, you need to check if the day is 11 and the month 3. Additionally, you have = operator on the last comparison instead of a == :在第二个条件下,您需要检查日期是否为 11 和月份3。此外,您在最后一个比较中使用=运算符而不是==

if ((month == "11" && day == "3") || (month == "3" && day == "11")) {

You should add "(" after if and add ")" after (day == "3" && month == "11") .Also change = operator on the last comparison to == and change last comparison from (day == "3" && month == "11") to (month == "3" && day == "11") .您应该在 if 之后添加 "(" 并在(day == "3" && month == "11")之后添加 ")" 。还将最后一次比较中的 = 运算符更改为 == 并将最后一次比较从(day == "3" && month == "11")(month == "3" && day == "11") Change your code to this code:将您的代码更改为此代码:

 function birthday (month,day){ let result; if( (month == "11" && day == "3") || (month == "3" && day == "11")){ result = "How did you know?"; } else { result = "Just Another Day"; } return result; } console.log(birthday(3, 11));

I modify your code, the last comparison symbol (==) was wrong, it was an asignment symbol(=) and you are comparing day and month twice, now your code works fine!我修改了你的代码,最后一个比较符号(==)是错误的,它是一个赋值符号(=),你正在比较日和月两次,现在你的代码可以正常工作了!

 function birthday (month, day){ let result = ""; if(( month == "11" && day == "3") || (month == "3" && day == "11")){ result = "How did you know?"; } else { result = "Just Another Day"; } return result; } console.log(birthday(3, 11));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM