[英]How to if on two conditions where one can be either of several values
I want to check that:我想检查一下:
if the state is true and the connection type is not 2 or 6 then do something.如果状态为真且连接类型不是 2 或 6,则执行某些操作。
if state is false and connection type is either 2 or 6 then do something else.如果 state 为 false 且连接类型为 2 或 6,则执行其他操作。
I have the following if statements but at points it is using the wrong code:我有以下 if 语句,但有时它使用了错误的代码:
async setWiFiConnection(state){
try{
var connType = await driver.getNetworkConnection();
if(state == true && connType != 2 && connType != 6){
await driver.toggleWiFi();
}else if(state == false && connType == 2 || connType == 6){
await driver.toggleWiFi();
}
}catch(err){
console.log(err);
}
}
I think the issue is if the connection type is 6 even if state is true then the else if code is being executed.我认为问题是如果连接类型为 6,即使 state 为真,则 else if 代码正在执行。
Dave Newton's answer was what I was after. Dave Newton 的回答正是我所追求的。 I needed to stick the connType checks in brackets see below:
我需要将 connType 检查粘贴在括号中,如下所示:
async setWiFiConnection(state){
try{
var connType = await driver.getNetworkConnection();
if(state == true && connType != 2 && connType != 6){
await driver.toggleWiFi();
}else if(state == false && (connType == 2 || connType == 6)){
await driver.toggleWiFi();
}
}catch(err){
console.log(err);
}
}
Thanks Dave谢谢戴夫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.