简体   繁体   中英

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

I am a beginner in Javascript. I did some research and still cannot figure out how to return the sum of array1 and add it to each number in array2 . My code adds the sum of both arrays instead. I want the expected log to be [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]

  • You can get sum of array using Array.reduce function.
  • And can add sum to each item of array2 using Array.map function.

 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]));

You need two separate loops. One loop calculates the sum of array1 . The second loop adds that to every element of array2 .

You can't combine the loops because you need the entire sum of the first array to add to the first element of the second array. Your code just adds the current value of the running total, not the final total. It's also adding elements of both arrays to sum , not adding sum to the elements of the second array.

Also, you're returning the sum, not the new array.

 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]

I think this will help you.

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]

There is a logical issue in your code. The reason why your code behaves wrongly is that you are trying to do, in the same for cycle, two activities that should be performed in different cycles. I'll try to explain it directly with the code:

// WHAT YOU SHOULD DO:

 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]

//WHAT YOU HAVE DONE:

 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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