简体   繁体   English

两个条件的三元运算符

[英]Ternary operator for two conditions

I wrote the basic javascript which take year and check conditions whether it is leap year or not how can I make ternary operator like that我写了基本的javascript,它需要年份并检查条件是否是闰年我怎样才能像这样制作三元运算符

gYear=prompt("enter the year");
let abc = (yr) => {
  alert((yr / 4 === 0 && !(yr / 100) === 0 )? "leap year" : "not leap year");
};
abc(gYear);

I find it's usually best to not even use the ternary operator.我发现通常最好甚至不使用三元运算符。 Some languages don't even have one:有些语言甚至没有:

 function isleapYear(year) { return year % 4 === 0 && year % 100 !== 0; } for (let y of [2021, 2020, 2000]) { let b = isleapYear(y); console.log(b); }

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

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