I need to calculate the sum of multiple arrays.
For example, I have a 2D array with an initial length of 2, but the length will change dynamically. Each element must have a length of 3.
function addSum(){
let length =3;
//calculate sum of two arrays
let arrayGroup = [[1,2,3],[1,2,3]]
for(let i=0; i< length ;i++){
for(let j=0; j< length ;j++){
let sum = arrayGroup[0][i] + arrayGroup[1][j]
console.log(sum)
}
}
}
//result in console should look like:
//2
//3
//4
//3
//4
//5
......and so on
Additional Info:
arrayGroup = [[1,2,3],[1,2,3]]
1 st sum= 1+1
2 nd sum= 1+2
3 rd sum= 1+3
4 th sum= 2+1
5 th sum= 2+2
6 th sum= 2+3
7 th sum= 3+1
8 th sum= 3+2
9 th sum= 3+3
These are all the sums of a 2-element array
if arrayGroup is a 3-element array [[1,2,3],[1,2,3],[1,2,3]]
1 st sum= 1+1+1 // arrayGroup[0][0] + arrayGroup[1][0] + arrayGroup[2][0]
2 nd sum= 1+1+2
3 rd sum= 1+1+3
4 th sum= 1+2+1
5 th sum= 1+3+1
6 th sum= 2+1+1
7 th sum= 3+1+1
....and so on, until listing all combinations
The code above demonstrates how I obtain results for two elements, however, when the number of elements in arrayGroup becomes N, I am looking for a way to get the results for N elements (i.e. arrayGroup = [[1,2,3],[1,2,3],.......,[1,2,3]] ).