We don’t allow questions seeking recommendations for software libraries, tutorials, tools, books, or other off-site resources. You can edit the question so it can be answered with facts and citations.
Closed 2 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(2)
如果x为0,则第一个
n
将是假的,否则真相。如果论点是偶数而不是0:
如果论点奇怪而积极,则为真实:
参数均匀,而0:
但是当参数奇怪和负面时,代码不起作用,因为负数奇数
%2
给出-1,而不是1。MFirst
n
will be falsey if x is 0, and truthy otherwise.If the argument is even and not 0:
If the argument is odd and positive:
If the argument is even and 0:
But the code doesn't work when the argument is odd and negative, because a negative odd number
% 2
gives -1, not 1.!n
是红鲱鱼。对于x
的任何值,0
,!n 将为false
;当x == 0
时,它将是true
。m
是0
偶数1
奇数数字。当您将数字与布尔值进行比较时,布尔值将转换为一个数字。因此,m< = false
等效于m< = 0
。当m
是0
时,这将是正确的。因此,您可以摆脱
n
,只需使用以下操作:之所代码>和
m == 0
。0< = true
是正确的,因此该功能也返回正确的结果。!n
is a red herring. For any value ofx
other than0
,!n
will befalse
; it will betrue
whenx == 0
.m
is0
for even numbers,1
for odd numbers. When you compare a number with a boolean, the boolean is converted to a number. Som <= false
is equivalent tom <= 0
. This will be true whenm
is0
.So you can get rid of
n
and just use this:The reason why it works with
!n
is because in the only case where!n
istrue
,x == 0
andm == 0
.0 <= true
is true, so the function returns the correct result then as well.