- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-plus-array.js
More file actions
Latest commit
18 lines (12 loc) · 695 Bytes
/
Copy patharray-plus-array.js
File metadata and controls
18 lines (12 loc) · 695 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//I'm new to coding and now I want to get the sum of two arrays...actually the sum of all their elements. I'll appreciate for your help.
//P.S. Each array includes only integer numbers. Output is a number too.
//P: Only takes in numbered arrays
//R: Returns a single number which is all the array integers added together
//E:
// arrayPlusArray([1, 2, 3], [4, 5, 6]), 21);
// arrayPlusArray([-1, -2, -3], [-4, -5, -6]), -21);
// arrayPlusArray([0, 0, 0], [4, 5, 6]), 15);
//P: Get the sum of each array via reduce, then add them together to get the total number.
functionarrayPlusArray(arr1,arr2){
returnarr1.reduce((acc,c)=>acc+c,0)+arr2.reduce((acc,c)=>acc+c,0)
}