简体   繁体   English

比较两个数组,无法使其返回true

[英]Comparing two arrays and can't get it to return true

My objective is to compare the two arrays. 我的目标是比较两个数组。 I can't get it return false, it just compares the first values and returns. 我不能让它返回false,它只是比较第一个值并返回。 Am I not asking it correctly to loop through all values before determining true or false? 我是否在确定是非题之前不要求它正确遍历所有值?

var array1 =[1,2,3];
var array2=[1,2,4];

var areArraysSame = function(array1, array2) {
   if(array1.length !== array2.length) {
       return "These are not the same length";
   }
   for(var i = 0; i <= array1.length; i++) {
      if(array1[i] !== array2[i]) {
      return "These are not the same array";

      }else{
      return "these are the same array";
      }
     }
    };
   console.log(areArraysSame(array1, array2));

You are not waiting for all comparisons to take place before returning true ( or your true string ) 在返回true或true字符串 )之前,您不必等待所有比较发生

You should do this: 你应该做这个:

for(var i = 0; i <= array1.length; i++) {
    if(array1[i] !== array2[i]) {
        return false;
    }
}
return true;

Wait for the loop to end before returning true 等待循环结束,然后返回true

You are returning immediately after checking the first value, remove the else block in the for loop such that it compares all the values as shown below : 检查第一个值后,您将立即返回,删除for循环中的else块,以便它比较所有值,如下所示:

var array1 =[1,2,3];
var array2=[1,2,4];

var areArraysSame = function(array1, array2) {
 if(array1.length !== array2.length) {
   return "These are not the same length";
 }
for(var i = 0; i <= array1.length; i++) {
  if(array1[i] !== array2[i]) {
   return "These are not the same array";
  }
 }
 return "These are the same array";
};
console.log(areArraysSame(array1, array2));

Return will stop your loop and exit the function. Return将停止循环并退出该函数。 So since in the above code your first element for both arrays is 1 the code returns on the first iteration of the loop. 因此,由于在上面的代码中,两个数组的第一个元素均为1,因此代码在循环的第一次迭代中返回。 If you want to return whether or not the arrays match return after the loop and use a boolean variable to keep track of whether or not the arrays match. 如果要返回数组是否匹配,请在循环后返回,并使用布尔变量来跟踪数组是否匹配。 Only return in the loop on a false statement. 仅在错误语句的循环中返回。

You have a return statement in the first iteration of your for loop; 在for循环的第一个迭代中有一个return语句; after the first item in each array is compared your loop will end whether it's true or false. 比较每个数组中的第一项之后,无论是真还是假,循环都会结束。

You can store a condition and base your return on that. 您可以存储条件,并以此为基础进行回报。

    var array1 = [1, 2, 3];
    var array2 = [1, 2, 4];

    var areArraysSame = function (array1, array2) {
        var isSame = true;

        if (array1.length !== array2.length) {
            return "These are not the same length";
        }

        for (var i = 0; i <= array1.length; i++) {
            if (array1[i] !== array2[i]) {
                isSame = false;
            }
        }
        return isSame ? "These are the same array" : "These are not the same array";
    };

    console.log(areArraysSame(array1, array2));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM