0

I want to build a function that

  1. takes an array of numbers as an input.
  2. calculates the running sum of the numbers in the input array.
  3. 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])
);
jsN00b
  • 3,584
  • 2
  • 8
  • 21
Yoshi
  • 111
  • 6
  • 2
    Seems to return an array for me (although it's missng the first number)? How are you checking for a return value? – Nick Parsons Jun 05 '22 at 08:30
  • 2
    I've run the code as well, it just seems to be missing the first element. – Ryan Zhang Jun 05 '22 at 08:31
  • 2
    @OP - we can use snippets to post code that can be executed within SO. As noted by both community members above, the code does generate an array (although it misses the first element, `1`). However, the code alters (ie, updates) the `nums` array by changing the elements in-place; which, in general, is not viewed as a positive, as per my limited understanding. – jsN00b Jun 05 '22 at 08:51
  • Does this answer your question? [Creating an array of cumulative sum in javascript](https://stackoverflow.com/questions/20477177/creating-an-array-of-cumulative-sum-in-javascript) – pilchard Jun 05 '22 at 10:14
  • Thank you for all of your support. I don't know why, but when I first ran it, it returned an empty array, but now it returns an array with numbers in it but missing the first element, just like everyone said. – Yoshi Jun 06 '22 at 03:30

4 Answers4

1

const input =  [1,2,3,4]

const theFunctionYouWannaWrite = (acc, v) => {
  acc.push(v + (acc[acc.length - 1] || 0))
  return acc;
}

const output = input.reduce(theFunctionYouWannaWrite, [])

console.log(output)

if you want to use it with for

const input =  [1,2,3,4]

const theFunctionYouWannaWrite = (acc, v) => {
  acc.push(v + (acc[acc.length - 1] || 0))
  return acc;
}

function sumOfArray(nums) {

  var output = [];

  for(let i = 0 ; i < nums.length ; i++) {
    theFunctionYouWannaWrite(output, nums[i])
  }

  return output;
};

console.log(sumOfArray(input))
Teneff
  • 30,564
  • 13
  • 72
  • 103
1

Just add first item in array initialization and everything works fine.

function sumOfArray(nums) {
  var output = [nums[0]];        // 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])
);
Thallius
  • 2,482
  • 2
  • 18
  • 36
  • OH thanks! Your solution is so simple that I am embarrassed not to think of it. – Yoshi Jun 06 '22 at 05:49
  • @Yoshi **Note:** If `nums` is an empty array then the function would return `[ undefined ]` – SSM Jun 06 '22 at 05:57
1

let 
  sum = 0,
  nums = [1, 2, 3, 4],
  res = [];

nums.forEach((item, index, arr) => {
  sum += arr[index];
  res.push(sum);
});

console.log(res);
SSM
  • 2,855
  • 1
  • 3
  • 10
Sevada 797
  • 346
  • 1
  • 8
1

You can do it using Array.prototype.reduce.

function sumOfArray(nums) {
  return nums.reduce((s, n) => (s.push((s.at(-1) ?? 0) + n), s), []);
}

console.log(sumOfArray([1, 2, 3, 4]));

And the problem with your solution is that you need start the loop from index 0 and check if the i-1th index is valid using either ?? or ||.

function sumOfArray(nums) {
  const output = [];
  for (let i = 0; i < nums.length; i++) {
    nums[i] = nums[i] + (nums[i - 1] ?? 0);
    output.push(nums[i]);
  }
  return output;
}

console.log(sumOfArray([1, 2, 3, 4]));

Other relevant documentations:

SSM
  • 2,855
  • 1
  • 3
  • 10