I want to build a function that
- takes an array of numbers as an input.
- calculates the running sum of the numbers in the input array.
- returns a new array that contains the running sum.
For example, if I call this function with the input of [1,2,3,4], the output should be [1,3,6,10]. However, when I run my code, it returns an empty array.
The following is the code I got so far.
function sumOfArray(nums) {
var output = []; // tip: initialize like let output = [nums?.[0] ?? 0];
for(let i = 1 ; i < nums.length ; i++) {
nums[i] = nums[i] + nums[i-1];
output.push(nums[i]);
}
return output;
};
console.log(
'calling sumOfArray([1,2,3,4])',
sumOfArray([1,2,3,4])
);