簡體   English   中英

如何將數組 1 的總和添加到數組 2 中的每個元素

[英]How do I add the sum of array 1 to each element in array 2

我是 Javascript 的初學者。 我做了一些研究,但仍然無法弄清楚如何返回array1的總和並將其添加到array2每個數字。 我的代碼添加了兩個數組的總和。 我希望預期的日志是[11, 13, 15]

 function addingAllTheWeirdStuff(array1, array2){ let list = [] let sum = 0 for (let i = 0; i < Math.max(array1.length, array2.length); i++){ list.push(sum += array1[i]) sum += array2[i] } return sum } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

  • 您可以使用Array.reduce函數獲取數組的總和。
  • 並且可以使用Array.map函數將 sum 添加到 array2 的每一項。

 function addingAllTheWeirdStuff(array1, array2){ const sum = array1.reduce((sum, cur) => sum + cur, 0); return array2.map(item => item + sum); } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6]));

您需要兩個單獨的循環。 一個循環計算array1的總和。 第二個循環將其添加到array2每個元素。

您不能組合循環,因為您需要將第一個數組的整個總和添加到第二個數組的第一個元素中。 您的代碼只是添加了運行總計的當前值,而不是最終總計。 它還將兩個數組的元素添加到sum ,而不是將sum添加到第二個數組的元素。

此外,您返回的是總和,而不是新數組。

 function addingAllTheWeirdStuff(array1, array2) { let sum = 0; for (let i = 0; i < array1.length; i++) { sum += array1[i]; } return array2.map(el => el + sum); } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

我認為這會對你有所幫助。

function addingAllTheWeirdStuff(array1, array2) {
        //firt get total of first array with reduce function
        const total = array1.reduce((accumulator, item) => {
            return accumulator + item;
        }, 0)
        const list = []
        //then add total to second function with map function.
        array2.map((e) => list.push(e + total))
        return list
    }
    
    console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

您的代碼中存在邏輯問題。 您的代碼行為錯誤的原因是您試圖在同一個 for 循環中執行兩個應該在不同循環中執行的活動。 我會嘗試直接用代碼來解釋它:

// 你應該做什么:

 function addingAllTheWeirdStuff(array1, array2){ let sum = 0; //First compute the sum of the first array for (let i = 0; i < array1.length; i++){ sum += array1[i]; //this is equivalent to sum=sum+array[i] } //Then add the sum to each element of array 2: for (let i = 0; i < array1.length; i++){ array2[i]+=sum; } //Finally, return the updated array return array2; } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

//你做了什么:

 function addingAllTheWeirdStuff(array1, array2){ let list = [] let sum = 0 //if array1.lenght!==array2.lenght this cycle will cause problems for (let i = 0; i < Math.max(array1.length, array2.length); i++){ //at each cycle of the for you are saving the value of sum //in list. It's not useful to compute sum. list.push(sum += array1[i]) //Here you are doing: sum = sum+array2[i]; //and not array2[i]=array2[i]+sum; //furthermore this should be done in a different for cycle sum += array2[i] } return sum } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

 function addingAllTheWeirdStuff(array1, array2) { if(array1 == null || array2 == null) return; const sumArray1 = array1.reduce((list, value) => list + value); return array2.map(value => value + sumArray1); } console.log(addingAllTheWeirdStuff([1, 3, 5], [2, 4, 6])); // expected log [11, 13, 15]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM