for 循环中的 if 语句都执行,而不仅仅是一个

if statements in for loop both executing instead of just one

本文关键字:不仅仅是 一个 执行 循环 if 语句 for      更新时间:2023-09-26

First 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

如果$scope.changeBack();做了一些修改discountcodes[i]$rootScope.discountcodeinput.attempt的事情,那么第二个if语句就可以执行。您可能想做的是:

for (var i=0; i<$scope.discountcodes.length; i++) {
    if($rootScope.discountcodeinput.attempt.toLowerCase()!=$scope.discountcodes[i]){
        $scope.changeBack();//DOES ONLY IF INPUT DOES NOT MATCH DATA
    }
    else {
        $scope.changeBackAgain();
    }
}

显然,由于内部的类型强制,使用 == 运算符可能会产生非常意外的结果,因此始终建议使用 ===

如果您不需要同时执行它们,还要在其中添加一个 else 语句。

除此之外,我会

说某些东西正在发生变化,我会在调试窗口中逐步浏览它,看看每个变量的当前结果是什么,或者将它们打印到控制台。

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
      }
  else if($rootScope.discountcodeinput.attempt.toLowerCase()===$scope.discountcodes[i]){
        console.log($scope.discountcodes[i]);//DOES ONLY IF INPUT MATCHES DATA
        $scope.changeBackAgain();
      }
    }
}

尝试使用 else if 语句运行一个代码,然后运行另一个代码

    <script>
    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]){
            console.log($scope.discountcodes[i]);//DOES ONLY IF INPUT MATCHES DATA
            $scope.changeBackAgain();
        } else if($rootScope.discountcodeinput.attempt.toLowerCase()!=$scope.discountcodes[i]){
            $scope.changeBack();
        }
    }
}
</script>

显然($rootScope.discountcodeinput.attempt.toLowerCase()!=$scope.discountcodes[i]($rootScope.discountcodeinput.attempt.toLowerCase()==$scope.discountcodes[i]不可能同时为真。所以我想你正在$scope.changeBack()里面做一些魔术,改变$rootScope.discountcodeinput.attempt$scope.discountcodes[i]的值,所以当第二个if被选中时,值已经改变,条件通过。

我建议在你知道你已经完成之后立即打破迭代,添加一个continue语句。像这样:

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
    continue; // done
  }
  // no need to check again
  console.log($scope.discountcodes[i]); //DOES ONLY IF INPUT MATCHES DATA
  $scope.changeBackAgain();
}

我想通了...

本质上,for 循环正在检查每个对象的 if 语句,如果第一个对象匹配,它将触发第一个 if 语句而不是第二个。但是然后 for 循环将继续并查看数组中的下一个对象,并且只触发第二个 if 语句......然后它将循环访问数组中的第三个对象,并再次只触发第二个 if 语句......等等等等。

我想出的解决方法只是将两者嵌入到另一个检查 var 值为 yes 的 if 语句中

if($scope.value=="no")

然后我把不等于的if语句(!=)放在第一位,把相等的if语句放在(==)第二位。最后,我在 (==) if 语句中添加了:

$scope.value="yes";

从而避免在 for 循环的每个循环上通过父 if 语句。

谢谢大家的帮助...这简直要了我的命!