gpt4 book ai didi

javascript - boolean 相等

转载 作者:行者123 更新时间:2023-12-02 07:27:57 26 4
gpt4 key购买 nike

我是从一道考试题中得出这个答案的,但无法理解该解决方案的工作原理。如果值“x”和“y”相等,则此函数应该返回“true”,否则返回 False。

解决方法:

function equal_boolean (x , y) {
return x ? y : y ? x : true;
}

为什么会这样?据我所知,结果是评估 X 是否为真。如果 X 为真,它将返回 Y。X 应该如何为“真”?

如果不是,它将评估 Y 是否为真,如果为真,则返回 X,如果不是,则返回 True。

我的理解有问题吗?

最佳答案

return x ? y : y ? x : true;

解析为

if x
return y // if y is true, then x == y. if y is false, then x != y
else (x is false)
if y
return x // x is false and y is true, y != x, return false
else
return true // x is false and y is false, return true

这当然是一种非常复杂的表达 boolean 相等性的方式(又名 Logical biconditional 又名 iff)。更自然的是这样的表达:

 (x && y) || (!x && !y)

关于javascript - boolean 相等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25710972/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com