I'm working on a problem that requires the running sum within the array. Example, an array with the values [1,2,3,4] should return [1,3,6,10] and an array that holds [3,1,2,10,1] should output [3,4,6,16,17]. Right now I am stumped on what I am missing for my answer. I am thinking that I should handle this recursively in some way, but I am not sure how to begin with that. In any case, this is my current solution
var runningSum = function(nums) { let result = []; nums.forEach(function(num, idx) { if (idx === 0) { result.push(num); } else { result.push(num + nums[idx - 1]); } }); return result; } Leet code stated that this was a fairly easy problem, so I figured that I'm thinking way too hard on this and can solve it a lot easier. Let me know your thoughts. Thanks!
num + nums[idx - 1]tonum + result[idx - 1]so that you actually take into the account the sum of previous numbers and not just the last numberfunction runningSum(nums) { let temp = 0; return nums.map(el => (temp += el, temp)); }Example: wandbox.org/permlink/iOJnh6FR1cCBSu6z