第一个 If 语句工作得很好,然后它按预期进入 for 循环,但是突然,它决定执行两个 if 语句(它们彼此矛盾“==”和“!=”) ..我不明白:(
if($scope.firstinitial==$scope.firstinput&&$scope.secondinitial==$scope.secondinput){
$scope.initialmatches="MATCHES THE INITIALS";
for (var i=0; i<$scope.discountcodes.length; i++) {
console.log($scope.discountcodes[i]);
if($rootScope.discountcodeinput.attempt.toLowerCase()!=$scope.discountcodes[i]){
$scope.changeBack();//DOES ONLY IF INPUT DOES NOT MATCH DATA
}
if($rootScope.discountcodeinput.attempt.toLowerCase()==$scope.discountcodes[i]){
console.log($scope.discountcodes[i]);//DOES ONLY IF INPUT MATCHES DATA
$scope.changeBackAgain();
}
}
}
更新: 经过错误测试后,只有最后一个折扣代码在匹配时才能正确工作……这让我更加困惑。因此,当用户输入与数组中最后一个折扣代码相匹配的折扣代码时,只有第一个 if 语句才会触发,而不会触发第二个。有什么想法吗?
最佳答案
我认为 $scope.changeBack();
内发生的任何事情都会使第二个条件true
关于javascript - for 循环中的 if 语句同时执行,而不是只执行一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36704408/