Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/470.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将两个数组中的每个值相加_Javascript_Arrays_Sum_Element - Fatal编程技术网

Javascript 将两个数组中的每个值相加

Javascript 将两个数组中的每个值相加,javascript,arrays,sum,element,Javascript,Arrays,Sum,Element,我试图理解这个条件循环来总结2个数组中的每个元素,但我无意中发现了后一部分,我不明白在这里要实现什么 你能给我解释一下吗 function Arrays_sum(array1, array2) { var result = []; var ctr = 0; var x = 0; if (array1.length === 0) return "array1 is empty"; if (array2.length === 0)

我试图理解这个条件循环来总结2个数组中的每个元素,但我无意中发现了后一部分,我不明白在这里要实现什么

你能给我解释一下吗

function Arrays_sum(array1, array2) {
    var result = [];
    var ctr = 0;
    var x = 0;

    if (array1.length === 0)
        return "array1 is empty";
    if (array2.length === 0)
        return "array2 is empty";

    while (ctr < array1.length && ctr < array2.length) {
        result.push(array1[ctr] + array2[ctr]);
        ctr++;
    }

    if (ctr === array1.length) //I don't understand from here onwards
    {
        for (x = ctr; x < array2.length; x++) {
            result.push(array2[x]);
        }
    } else {
        for (x = ctr; x < array1.length; i++) {
            result.push(array1[x]);
        }
    }

    return result;
}
函数数组\u和(array1,array2){
var结果=[];
var-ctr=0;
var x=0;
if(array1.length==0)
返回“array1为空”;
if(array2.length==0)
返回“array2为空”;
while(ctr
让我们假设以下两个数组及其“和”:

注意最后两项。总和等于第一个数组的值,因为第二个数组不包含这样数量的值

Array 1: 1  2  3  4   5  6   7   8  9
Array 2: 2  4  6  8   2  4   6   ?  ?
这正是算法的作用:

1) 而这两个数组在
i
index-sum处都有数字

ctr    : !  !  !  !   !  !   !  \|/   [ ctr   = 7 (remember: 0-based indexes)]       
Array 1: 1  2  3  4   5  6   7   8  9 [length = 9]
Array 2: 2  4  6  8   2  4   6        [length = 7]

Sum    : 3  6  9  12  7  10  13  
此处
while(ctr
条件在
ctr
处中断

此外,检查
ctr==array2.length
返回数组2结束的真正含义,我们需要继续遍历
array1

for (x = ctr; x < array1.length; i++) {
    result.push(array1[x]);
}

让我们假设以下两个数组及其“和”:

注意最后两项。总和等于第一个数组的值,因为第二个数组不包含这样数量的值

Array 1: 1  2  3  4   5  6   7   8  9
Array 2: 2  4  6  8   2  4   6   ?  ?
这正是算法的作用:

1) 而这两个数组在
i
index-sum处都有数字

ctr    : !  !  !  !   !  !   !  \|/   [ ctr   = 7 (remember: 0-based indexes)]       
Array 1: 1  2  3  4   5  6   7   8  9 [length = 9]
Array 2: 2  4  6  8   2  4   6        [length = 7]

Sum    : 3  6  9  12  7  10  13  
此处
while(ctr
条件在
ctr
处中断

此外,检查
ctr==array2.length
返回数组2结束的真正含义,我们需要继续遍历
array1

for (x = ctr; x < array1.length; i++) {
    result.push(array1[x]);
}

让我们假设以下两个数组及其“和”:

注意最后两项。总和等于第一个数组的值,因为第二个数组不包含这样数量的值

Array 1: 1  2  3  4   5  6   7   8  9
Array 2: 2  4  6  8   2  4   6   ?  ?
这正是算法的作用:

1) 而这两个数组在
i
index-sum处都有数字

ctr    : !  !  !  !   !  !   !  \|/   [ ctr   = 7 (remember: 0-based indexes)]       
Array 1: 1  2  3  4   5  6   7   8  9 [length = 9]
Array 2: 2  4  6  8   2  4   6        [length = 7]

Sum    : 3  6  9  12  7  10  13  
此处
while(ctr
条件在
ctr
处中断

此外,检查
ctr==array2.length
返回数组2结束的真正含义,我们需要继续遍历
array1

for (x = ctr; x < array1.length; i++) {
    result.push(array1[x]);
}

让我们假设以下两个数组及其“和”:

注意最后两项。总和等于第一个数组的值,因为第二个数组不包含这样数量的值

Array 1: 1  2  3  4   5  6   7   8  9
Array 2: 2  4  6  8   2  4   6   ?  ?
这正是算法的作用:

1) 而这两个数组在
i
index-sum处都有数字

ctr    : !  !  !  !   !  !   !  \|/   [ ctr   = 7 (remember: 0-based indexes)]       
Array 1: 1  2  3  4   5  6   7   8  9 [length = 9]
Array 2: 2  4  6  8   2  4   6        [length = 7]

Sum    : 3  6  9  12  7  10  13  
此处
while(ctr
条件在
ctr
处中断

此外,检查
ctr==array2.length
返回数组2结束的真正含义,我们需要继续遍历
array1

for (x = ctr; x < array1.length; i++) {
    result.push(array1[x]);
}

在第一个循环结束时,以下断言有效:

ctr === array1.length || ctr === array2.length
请遵守以下场景说明:

array1: xxxxxxx    (length = 7)
array2: xxxxxxxxxx (length = 10)
               ^
               7 (ctr)
如果到达一个数组的末尾,它将从该点开始继续追加另一个数组的每个元素。当然,这假设每个数组边界之外的元素被认为是
0

从技术上讲,通过考虑以下因素,该功能可以减少边缘情况:

array2 is empty => yield array1
array1 is empty => yield array2
也就是说,因为我们处理的是JavaScript,所以我们可以稍微改变CS规则:

function array_sum(a, b)
{
  // iterate over the biggest array and map each value
  return (a.length > b.length ? a : b).map(function(value, index) {
    // to the sum of both, whereby 0 is assumed for non-existent elements
    return value + (b[index] || 0);
  });
}

在第一个循环结束时,以下断言有效:

ctr === array1.length || ctr === array2.length
请遵守以下场景说明:

array1: xxxxxxx    (length = 7)
array2: xxxxxxxxxx (length = 10)
               ^
               7 (ctr)
如果到达一个数组的末尾,它将从该点开始继续追加另一个数组的每个元素。当然,这假设每个数组边界之外的元素被认为是
0

从技术上讲,通过考虑以下因素,该功能可以减少边缘情况:

array2 is empty => yield array1
array1 is empty => yield array2
也就是说,因为我们处理的是JavaScript,所以我们可以稍微改变CS规则:

function array_sum(a, b)
{
  // iterate over the biggest array and map each value
  return (a.length > b.length ? a : b).map(function(value, index) {
    // to the sum of both, whereby 0 is assumed for non-existent elements
    return value + (b[index] || 0);
  });
}

在第一个循环结束时,以下断言有效:

ctr === array1.length || ctr === array2.length
请遵守以下场景说明:

array1: xxxxxxx    (length = 7)
array2: xxxxxxxxxx (length = 10)
               ^
               7 (ctr)
如果到达一个数组的末尾,它将从该点开始继续追加另一个数组的每个元素。当然,这假设每个数组边界之外的元素被认为是
0

从技术上讲,通过考虑以下因素,该功能可以减少边缘情况:

array2 is empty => yield array1
array1 is empty => yield array2
也就是说,因为我们处理的是JavaScript,所以我们可以稍微改变CS规则:

function array_sum(a, b)
{
  // iterate over the biggest array and map each value
  return (a.length > b.length ? a : b).map(function(value, index) {
    // to the sum of both, whereby 0 is assumed for non-existent elements
    return value + (b[index] || 0);
  });
}

在第一个循环结束时,以下断言有效:

ctr === array1.length || ctr === array2.length
请遵守以下场景说明:

array1: xxxxxxx    (length = 7)
array2: xxxxxxxxxx (length = 10)
               ^
               7 (ctr)
如果到达一个数组的末尾,它将从该点开始继续追加另一个数组的每个元素。当然,这假设每个数组边界之外的元素被认为是
0

从技术上讲,通过考虑以下因素,该功能可以减少边缘情况:

array2 is empty => yield array1
array1 is empty => yield array2
也就是说,因为我们处理的是JavaScript,所以我们可以稍微改变CS规则:

function array_sum(a, b)
{
  // iterate over the biggest array and map each value
  return (a.length > b.length ? a : b).map(function(value, index) {
    // to the sum of both, whereby 0 is assumed for non-existent elements
    return value + (b[index] || 0);
  });
}

正如您所看到的,您正在使用if,因为在while循环中,您正在使用&&condition检查两个数组

因此,如果假设array1.length=1array2.length=2以及ctr=1,则while循环将停止,但array2还有一个剩余值,因此对于剩余值,它们使用if