简体   繁体   English

JavaScript中的OR运算子

[英]OR operator in Javascript

message += days + " day" + ( days==1 ? '':'s' ) + ", ";

If my counter comes to 1 month and ZERO day, it writes dayS, of course. 如果我的计数器是1个月零零天,那它当然是dayS。 How can I pass a "OR" operator into this code? 如何将“ OR”运算符传递给此代码?

I tried: 我试过了:

( (days==1 ? '':'s' ) || (days==0 ? '':'s'))

and: 和:

( days==1||0 ? '':'s' )

Both of them show an S at value "0" 他们两个都显示值为“ 0”的S

why don't you try: 你为什么不尝试:

days < 2

unless there's a chance of negative number of days 除非天数可能为负

试试(( days==1|| days == 0) ? '':'s' )

"0 days" is correct, but if you really want it: (days == 0 || days == 1 ? '' : 's') “ 0天”是正确的,但是如果您真的想要它:( (days == 0 || days == 1 ? '' : 's')

Alternatively: (days <= 1 ? '' : 's') (since you can't have a negative or non-integer number of days. 或者: (days <= 1 ? '' : 's') (因为您的天数不能为负数或非整数)。

Or you could do this to the whole line: 或者您可以对整个行执行此操作:

if( days) message += days + " day" + (days==1 ? '' : 's') + ", ";

这样写

message += days + " day" + ( days<=1 ? '':'s' ) + ", ";

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

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