I'm quite knew to JavaScript (been studying it only for less than a week now) and am having problems computing the sum of the values in the subarrays I generated from a certain array. I was able to do that using this function:
function getSubs(arr) {
var newarr = [];
for (var i = 0; i < arr.length; i++){
for (var j = arr.length; j > 0; j--){
newarr.push(arr.slice(i, j));
}
}
return newarr;
}
Now if this function was invoked for example for the array [1,2,3]
, the result is [[], [], [], [1], [2], [3], [2, 3], [1, 2], [1, 2, 3]]
. I don't understand why there are three null arrays but this is close to what I'm trying to achieve. Additionally, I want to get the sums of the values in each subarray. I know the code above's very crude. Hope someone can help me improve it. Thanks in advance!