skip to Main Content
let sum = 0;
var runningSum = function (nums2) {
    let result = []
    while (result.length > 0) {
        result.pop();
    }
    for (let i = 0; i < nums2.length; i++) {
        sum += nums2[i];
        result.push(sum)
    }
    return result
};

I get the following output:

runningSum([1,1,1]) //=> [1, 2, 3]
runningSum([1,1,1]) //=> [4, 5, 6]

I’m expecting the following output:

runningSum([1,1,1]) //=> [1, 2, 3]
runningSum([1,1,1]) //=> [1, 2, 3]

What am I doing wrong?

7

Answers


  1. The problem is that each time runningSum is called, the variable sum is not reset to zero because it is defined outside of the runningSum function. This indicates that sum keeps the value from the prior call and keeps adding to the ongoing sum.

    var runningSum = function(nums2) {
      let sum = 0;
      let result = [];
      for (let i = 0; i < nums2.length; i++) {
        sum += nums2[i];
        result.push(sum);
      }
      return result;
    };
    console.log(runningSum([1, 1, 1]));
    console.log(runningSum([1, 1, 1]));
    Login or Signup to reply.
  2. As the comment point out, you need to define the variable sum locally in your function and you can get rid of the while.

    By defining sum within your function, then it will be created new every time the function is called.

    If you happen to need a variable named sum outside the function, you can still do so. Despite having the same name, they are completely different variable that do not affect one another:

    let sum = 0;
    var runningSum = function (nums2) {
            let sum = 0;
            let result = []
            for (let i = 0; i < nums2.length; i++) {
                sum += nums2[i];
                result.push(sum)
            }
            return result
        };
        console.log(sum) 
        console.log(runningSum([1,1,1]))
        sum = 123456789
        console.log(sum)
        console.log(runningSum([1,1,1]))
    Login or Signup to reply.
  3. You can simplify your code as it:

    function runningSum(nums2) {
      let sum = 0;
      return nums2.map(x => {
        sum += x;
        return sum;
      })
    }
    
    const arr1 = [1, 1, 1];
    const sum1  = runningSum(arr1);
    console.log(sum1)
    const arr2 = [1, 1, 1];
    const sum2  = runningSum(arr2);
    console.log(sum2)
    Login or Signup to reply.
  4. It’s great that others are simplifying your code (which should be done eventually), but this answer will show you why sum is not reset upon each call to the function. Since sum is set outside the function, the value is not reset. I moved it inside the function.

    var runningSum = function (nums2) {
        let sum = 0;
        let result = []
        while (result.length > 0) {
            result.pop();
        }
        for (let i = 0; i < nums2.length; i++) {
            sum += nums2[i];
            result.push(sum)
        }
        return result;
    };
    
    Login or Signup to reply.
  5. The problem is, you are declaring your sum outside of your function and never resetting it. So in your second call of runningSum you are already starting with a sum = 3 instead of sum = 0

    But actually, you don’t need that variable at all. For the value of result[i] just add the previous element in the result array and the current element in the input array. result[i] = result[i-1] + arr[i]. For index i = 0 you have to take precautions and add a fallback to 0, because result[-1] will give undefined

    function runningSum(arr) {
      let result = [];
      for (let i = 0; i < arr.length; i++)
        result[i] = (result[i-1] || 0) + arr[i];
      return result;
    }
    
    console.log(runningSum([]));
    console.log(runningSum([1]));
    console.log(runningSum([1,1,1]));
    console.log(runningSum([1,2,3]));
    Login or Signup to reply.
  6. just write something like that (let sum=0 goes inside function)

    var runningSum = function (nums2) {
        let result = []
        // u can write sum here
        let sum = 0;
        // this is u can remove this does nothing 
        // while (result.length > 0) {
        //     result.pop();
        // }
        for (let i = 0; i < nums2.length; i++) {
            sum += nums2[i];
            result.push(sum)
        }
        return result
    };
    
    // this is for test
    console.log(runningSum([1,1,1]))
    console.log(runningSum([1,1,1]))
    Login or Signup to reply.
  7. The sum variable should not exist globally (but within the function), otherwise it will never be reset to 0.

    Maybe you can simplify your function using a reducer, something like:

    const runningSum = (...values) => values
      .reduce( (acc, value) => acc.concat(value + (acc[acc.length-1] || 0)), []);
      
    console.log(`1, 1, 1 => ${JSON.stringify(runningSum(1,1,1))}`);
    const someValues = [1,3,5,7,9];
    console.log(`${someValues.join(`, `)} => ${
      JSON.stringify(runningSum(...someValues))}`);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search