[英]JavaScript OR Operator correct, but if statement not behaving as expected
I am working in a JavaScript heavy application, and I have the following code: 我正在使用JavaScript繁重的应用程序,并且有以下代码:
function displayingNotes() {
if (!($scope.overseasTravelTask.RegisteredKeeperName.toLowerCase()
.indexOf("zenith") > -1 ||
$scope.overseasTravelTask.RegisteredKeeperAddressLine1.toLowerCase()
.indexOf("zenith")
)
)
{
$scope.calloutClass = 'callout-danger';
$scope.calloutMessage = 'We (Zenith) cannot provide this vehicle with the necessary documents.';
$scope.disableNextBtn = true;
}
else
{
$scope.calloutMessage = 'Please ask driver about this.';
}
}
For the if statement , I am placing a break-point and checking my logic in the Chrome console, and logic looks correct, but when I click the F10 button, I expect to get inside the if statement, instead I jump over it! 对于if语句 ,我在Chrome控制台中放置了一个断点并检查了我的逻辑,并且逻辑看起来正确,但是当我单击F10按钮时,我希望进入if语句,而是跳过它!
The second operand to your ||
||
的第二个操作数 is: 是:
$scope.overseasTravelTask.RegisteredKeeperAddressLine1.toLowerCase().indexOf("zenith")
...which will result in a number: The index of the substring, or -1
if not found. ...这将产生一个数字:子字符串的索引;如果未找到,则为-1
。 So when coerced to a boolean, it will be true
for all cases (-1, 23, 42, etc.) other than when the substring happens to be found at index 0
, in which case it will be false
. 因此,当强制转换为布尔值,这将是true
为小于当子发生在索引被发现其它所有情况下(-1,23,42,等等) 0
,在这种情况下,这将是false
。
You probably wanted a !== -1
in there. 您可能希望在其中添加!== -1
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.