同一功能WIERD方式

发布于 2025-02-10 06:14:55 字数 1551 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱暴击 2025-02-17 06:14:56

如果x为0,则第一个

var n = x / 2

n将是假的,否则真相。

如果论点是偶数而不是0:

如果论点奇怪而积极,则为真实:

  • m将是1
  • (因为任何奇数数除以2是另一个非零的数字,而非零的数字是真实的)
  • !n将是错误的(因为那是真理价值的布尔值)
  • 返回语句为1< = false;与< =,1< = 0相比,false将转换为数字,

参数均匀,而0:

但是当参数奇怪和负面时,代码不起作用,因为负数奇数%2给出-1,而不是1。M

  • 将是-1
  • n将是真实的(因为任何 非零数字,而非零的数字是真实的)
  • 奇数数除以2是另一个 与< =,-1< = 0相比,false将转换为数字,当该数字为false 时,是正确的 -

First

var n = x / 2

n will be falsey if x is 0, and truthy otherwise.

If the argument is even and not 0:

  • m will be 0
  • n will be a truthy number (because anything divided by 2 is another non-zero number - except 0)
  • !n will be false (because that's the boolean inverse of 0, a falsey value)
  • the return statement is 0 <= false; false gets converted into a number when compared with <=, and 0 <= 0 is true

If the argument is odd and positive:

  • m will be 1
  • n will be truthy (because any odd number divided by 2 is another non-zero number, and a non-zero number is truthy)
  • !n will be false (because that's the boolean inverse of a truthy value)
  • the return statement is 1 <= false; false gets converted into a number when compared with <=, and 1 <= 0 is false

If the argument is even and 0:

  • m will be 0
  • n will be 0
  • !n will be true (boolean inverse of 0)
  • the return statement is 0 <= true; true gets converted into a number when compared with <=, and 0 <= 1 is true

But the code doesn't work when the argument is odd and negative, because a negative odd number % 2 gives -1, not 1.

  • m will be -1
  • n will be truthy (because any odd number divided by 2 is another non-zero number, and a non-zero number is truthy)
  • !n will be false (because that's the boolean inverse of a truthy value)
  • the return statement is -1 <= false; false gets converted into a number when compared with <=, and -1 <= 0 is true - when it should be false
还给你自由 2025-02-17 06:14:55

!n是红鲱鱼。对于x的任何值,0,!n 将为false;当x == 0时,它将是true

m0偶数 1奇数数字。当您将数字与布尔值进行比较时,布尔值将转换为一个数字。因此,m&lt; = false等效于m&lt; = 0。当m0时,这将是正确的。

因此,您可以摆脱n,只需使用以下操作:

function isEven(x){
   var m = x % 2;
   return m <= false
}

[0, 1, 2, 3, 4, 5, 6, 7, 8].forEach(x => console.log(x, isEven(x)));

之所代码>和m == 00&lt; = true是正确的,因此该功能也返回正确的结果。

!n is a red herring. For any value of x other than 0, !n will be false; it will be true when x == 0.

m is 0 for even numbers, 1 for odd numbers. When you compare a number with a boolean, the boolean is converted to a number. So m <= false is equivalent to m <= 0. This will be true when m is 0.

So you can get rid of n and just use this:

function isEven(x){
   var m = x % 2;
   return m <= false
}

[0, 1, 2, 3, 4, 5, 6, 7, 8].forEach(x => console.log(x, isEven(x)));

The reason why it works with !n is because in the only case where !n is true, x == 0 and m == 0. 0 <= true is true, so the function returns the correct result then as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文