用于JavaScript中的循环跳过索引

用于JavaScript中的循环跳过索引,javascript,angularjs,Javascript,Angularjs,我用JavaScript编写了一个简单而直接的for循环。但是,它跳过了第二个索引。我不明白为什么 下面是我的代码片段: if($scope.usersChoice.length == $scope.correctAnswers.length){ for(var p=0;p<$scope.usersChoice.length;p++) { if($scope.usersChoice[p] == $scope.correctAnswers[p

我用JavaScript编写了一个简单而直接的for循环。但是,它跳过了第二个索引。我不明白为什么

下面是我的代码片段:

if($scope.usersChoice.length == $scope.correctAnswers.length){             
    for(var p=0;p<$scope.usersChoice.length;p++) {
        if($scope.usersChoice[p] == $scope.correctAnswers[p]){
            $scope.score++;
        }
    }
}
if($scope.usersChoice.length==$scope.correctAnswers.length){

对于(var p=0;p你怎么知道它跳过了第二个指数?因为它不应该。 你能给我们看看你的阵列吗

顺便说一下,在这种情况下,您可以使用forEach而不是for循环,因为您似乎不需要中断循环:

if($scope.usersChoice.length == $scope.correctAnswers.length){  
    $scope.usersChoics.forEach($choice,$index=>{
        if($choice === $scope.correctAnswers[$index]) $scope.score++;
    });
}

你能显示你的数组吗?我使用了调试器并找到了。以下是我的数组:usersChoice:public static void main(String args[]),void不返回任何数据类型,上面所有的,True,A和B,public static void main(String args[]),Java支持过程和OOP,synchronized,int[]x[];更正答案:公共静态void main(String args[]),void不返回任何数据类型,上述所有内容,均为True,A和B,公共静态void main(String args[]),Java支持过程和OOP,synchronized,int[][]x[];等等,您取消了问题的标题“JavaScript中的循环跳过索引”但我知道你似乎在谈论Java,有不同的语言和不同的概念。你的评论也很神秘。所以,请先告诉我你是如何知道for循环跳过了一个元素的?我在“$scope.score++;”之后使用了调试器语句这一行。然后我发现变量p的值不是2。可能是因为条件语句
if($choice===$scope.correctAnswers[$index]))
在此索引处为false。因为您的代码片段完全正确。哦,避免使用
var
,在for循环中您可以使用
let
,它的作用域更合适。