We are required to write a JavaScript function that takes in an array of arrays of numbers. For each subarray, the function such create a partial sum subarray (an array in which a particular value is the sum of itself and the previous value).
For example −
If the input array is −
const arr = [ [1, 1, 1, -1], [1, -1, -1], [1, 1] ];
Then the output should be −
const output = [ [1, 2, 3, 2], [1, 0, -1], [1, 2] ];
Example
const arr = [ [1, 1, 1, -1], [1, -1, -1], [1, 1] ] const partialSum = (arr = []) => { const res = [] arr.forEach(sub => { let accu = 0 const nestedArr = [] sub.forEach(n => { accu += n; nestedArr.push(accu); }); res.push(nestedArr); }); return res; }; console.log(partialSum(arr));
Output
And the output in the console will be −
[ [ 1, 2, 3, 2 ], [ 1, 0, -1 ], [ 1, 2 ] ]