我找不到代码中可能缺少或错误的地方,无论如何,我已经创建了一个if else
语句,我可以在以下情况下转到第一个条件:
Variable One has a "N" value AND Variable Two or Variable Three has a "ValueOne"
或Variable One has a "N" AND Variable Four has a value of either "ValueTwo" or "ValueThree"
我的代码中的问题是,只要变量四具有正确的值,即使变量 1 中没有值,它也会进入第一个条件
您将在代码中看到的值不是我将使用的实际值,它只是一个示例。
我尝试使用此代码来解决我的问题:
function testNum() {
var accessEquipment = "n";
var sync = "sample1";
var fiberServiceAlarm = "sample1";
var wanLight = "flashingRed";
var result1 = "success";
var result2 = "fail";
if (
(accessEquipment == "n" && (sync == "sample" || fiberServiceAlarm == "sample")) ||
(accessEquipment == "n" && (wanLight == "flashingGreen" || wanLight == "off" || wanLight == "flashingRed"))) {
return result1;
} else {
return result2;
}
}
这解决了问题,但我认为以这种方式编写代码不是很好。有没有办法不使访问设备翻倍?
这是我想要工作的实际代码: 有什么需要补充的吗? 在这个中,它仍然转到 IF 语句事件访问设备与我设置的值不匹配,只要 wanLight 具有闪烁的红色或其他值,它仍然转到 IF 语句。我想要的是,只有当访问设备具有N并且wanLight具有三个值中的任何一个时,它才会通过那里。
function testNum() {
var accessEquipment = "na";
var sync = "sample1";
var fiberServiceAlarm = "sample1";
var wanLight = "flashingRed";
var result1 = "success";
var result2 = "fail";
if (accessEquipment == "n"
&& (sync == "sample" || fiberServiceAlarm == "sample")
|| (wanLight == "flashingGreen" || wanLight == "off" || wanLight == "flashingRed")
) {
return result1;
} else {
return result2;
}
}
需要添加一对括号
if (accessEquipment == "n"
&& ((sync == "sample" || fiberServiceAlarm == "sample")
|| (wanLight == "flashingGreen" || wanLight == "off" || wanLight == "flashingRed")))
您可以通过在数组中获取各种值并检查包含来获取更紧凑的版本。这适用于常量值和变量。
function testNum() {
var accessEquipment = "n";
var sync = "sample1";
var fiberServiceAlarm = "sample1";
var wanLight = "flashingRed";
var result1 = "success";
var result2 = "fail";
return accessEquipment == "n" && (
[sync, fiberServiceAlarm].includes("sample") ||
["flashingGreen", "off", "flashingRed"].includes(wanLight)
)
? result1
: result2;
}