Running Sum of 1D Array
In this lab, you will create a function to compute the running sum of a given 1D array. You’ll be given an input array nums
. The running sum of this array is defined as runningSum[i] = sum(nums[0]...nums[i])
. Your task is to write a function that will return the running sum of nums
.
Examples
-
Input: nums = [1, 2, 3, 4]
Output: [1, 3, 6, 10]
Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4]. -
Input: nums = [1, 1, 1, 1, 1]
Output: [1, 2, 3, 4, 5]
Explanation: Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1]. -
Input: nums = [3, 1, 2, 10, 1]
Output: [3, 4, 6, 16, 17]
Constraints
1 <= nums.length <= 1000
-10^6 <= nums[i] <= 10^6
Challenges
-
Implement the
runningSum
function to calculate the running sum of the given 1D arraynums
. Export this function using ESM export. -
Add an edge case check to your function to handle the case when the input
nums
array is empty. If the input array is empty, your function should return an empty array. Export this updated function using ESM export.
Evaluation Script
Make sure to export the runningSum
function from your solution as it will be imported and tested for correctness in the evaluation script.
Initial File System for User
/** * @param {number[]} nums * @return {number[]} */ var runningSum = function(nums) { };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js'] terminals: ['yarn install']
NOTE: Make sure you're
default export
ing the function ES Modules