educative.io

Educative

Sum of all the items in the sub-array

var arraySum = function(numbers) {

var sums = [];

var sum = 0;

var count = 0;

for (i = 0; i< numbers.length; i++){

  for (j = 0; j< numbers[i].length;j++){

sum = sum + numbers[i][j]}
strong text
sums[i] = sum;

** sum = 0;**
strong text
}

return sums;

}

Can you please explain the code placed in the strong text?

Hi Dinesh,
sums[] is an array to store the sum of each row of input array in each of its index like in the example provided in that lesson before. i.e., for
var numbers = [
[1, 2, 3, 4],
[5, 6, 7],
[8, 9, 10, 11, 12]
];
the sums[] array would be equal to [10, 18, 50].
Now, in the strong text sums[i] = sum means the sum that was calculated for ith row is being put in the ith index of sums[] array. e.g., for i=0, the sum is 10 (for above input array) so the 0th index of sums[] array will contain 10.
After that, sum = 0 is written to start the sum calculation of next row.