gpt4 book ai didi

javascript - 如何正确编码/调试复杂的 bool 条件

转载 作者:行者123 更新时间:2023-11-30 10:34:03 24 4
gpt4 key购买 nike

是否可以这样写一个if语句:

if(a === 0 && (b === 0 || c === 0)){
if(b === 0 && c === 0){
//a, b, and c are equal to 0
}
else if(b === 0){
//only a and b are equal to 0
}
else {
//only a and c are equal to 0
}
}
else {
//a doesn't equal 0, but b or c could so we test those
}

它似乎在我以类似方式编写的代码中不起作用......也许我写错了?这在我脑海中是有道理的。我该如何构建我的代码以避免这样的困惑?

最佳答案

我纠正了你的其他问题:

if(a === 0 && (b === 0 || c === 0)){
if(b === 0 && c === 0){
//a, b, and c are equal to 0
}
else if(b === 0){
//only a and b are equal to 0
}
else {
//only a and c are equal to 0
}
}
else {
// a === 0 and neither b nor c === 0,
//or a!==0 and neither b nor c === 0,
//or a!==0 and either b or c or both === 0
}

我的主张:

if(a === 0 && (b === 0 || c === 0)){
if(b === 0 && c === 0){
//a, b, and c are equal to 0
}
else if(b === 0){
//only a and b are equal to 0
}
else {
//only a and c are equal to 0
}
} else if (a === 0) {
//a === 0 and neither b nor c === 0,
} else {
// a!==0 and neither b nor c === 0,
//or a!==0 and either b or c or both === 0
}

另外,你可以考虑按位运算,可能会更清楚。萨鲁多斯,

关于javascript - 如何正确编码/调试复杂的 bool 条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15074050/

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