Having trouble understanding why this is the solution. Why is there a need for ‘new’? and why do ans[0] = nums[0]?

```
var runningSum = function(nums) {
let ans = new Array(nums.length)
ans[0] = nums[0]
for (let i = 1; i < nums.length; i++)
ans[i] = ans[i-1] + nums[i]
return ans
};
```

hbar1st
September 19, 2022, 3:26pm
2
just want to confirm , this is JavaScript right?

You may want to describe the problem in your own words.
This function calculates a running sum by …???..
(try to describe how you would do it by hand)

Did you come up with a solution of your own to compare to? I highly, highly recommend creating your own solution before looking up other people’s solutions.

lol I tried, but didn’t even think to do
`let ans = new Array(nums.length)`

If you did not solve it on your own, it is going to be hard for you to understand what is going on.

That one piece of syntax is not required to solve this problem.

I would try to solve this problem first.

That’s the thing – I COULDn’t solve it ): Here is why —

I kept trying to do

```
const runningSum=function(nums) {
for (let i=1; i<nums.length; i++) {
nums =nums[i]+nums[i];
}
return nums;
};
```

but I couldn’t figure out what I was doing wrong

If you were to use human words to describe what you are attempting to do on that line, what human words would you use?

Replacing nums with the index of nums plus the index of nums

But `nums`

is an array. You are replacing an array with a number. That’s not going to work.

oooooh! looks like nums[i] would work instead of nums. I think i got it

That’s closer, but you need the running sum .

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].

legend! thank you so much

system
Closed
March 21, 2023, 4:35am
15
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.