Find running sum of an array in JavaScript

0

Table of Contents

Problem

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Solution

var runningSum = function(nums) {
    let result = new Array();
    if(nums.length > 1){
        for(let i =0; i< nums.length; i++){
            let rsum = 0;
            for(let j = 0; j<(i+1); j++){
                rsum = rsum+nums[j];
            }
            result.push(rsum);
        }
    }
    return result;
};

Leave a Comment

Skip to content