1

I have a matrix :

matrix = [[0, 1, 1, 2],
          [0, 5, 0, 0],
          [2, 0, 3, 3]]

I need to calculate Sum of all array elements which are not under 0 So, in this example Sum should be = 9

I have this function:

function matrixElementsSum(matrix) {
  // Write your code here
 var Summa =0
  for (i = 0; i<4; i++){
      var sum =0;
    // console.log(matrix[i].length); //4
    for(j=0; j<matrix.length; j++){
      console.log("Matrix JI "+ matrix[j][i])
         sum = sum +matrix[j][i]; 
         if (matrix[j][i-1]!=0){
           console.log(matrix[j][i-1])
           Summa =Summa+ sum;
         }
    }
    console.log('-----------' +Summa)
    console.log("Sum "+sum);
  }

  return Summa;
}

i think i need to change if (matrix[j-1][i]!=0) but it doesn't work

Elena
  • 569
  • 3
  • 7
  • 19
  • I believe you can get started with this question and answer: https://stackoverflow.com/questions/1230233/how-to-find-the-sum-of-an-array-of-numbers Perhaps then step through each one at a time? – Aaron Belchamber Jun 23 '17 at 18:30
  • @AaronBelchamber Nah. This is a question from CodeFights, not a generic "how to sum an array". – Karl Reid Jun 23 '17 at 18:32

4 Answers4

2

You can use reduce() and inside forEach() loop for this. If the current element in foreach loop is zero then you can store index of that element in one other object zero and you can use that object to check if there was zero with same index.

var matrix = [
  [0, 1, 1, 2],
  [0, 5, 0, 0],
  [2, 0, 3, 3]
]

var zero = {}
var sum = matrix.reduce(function(r, e, i) {
  e.forEach(function(n, j) {
    if (n == 0) zero[j] = true;
    if (!zero[j]) r += n;
  })
  return r;
}, 0)

console.log(sum)
Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176
  • I'm a little confused on the mechanics here. I understand how reduce is adding the sum value, and how forEach is cycling through each individual array and filtering out the positive values. But how does forEach recognize the previous array had a zero in the same index spot? – Jleibham Dec 27 '17 at 04:46
1

You can sum 2 arrays and ignore numbers from the bottom array, which items from the same index on the top array are 0.

Now you can iterate the matrix from the end, and sum the resulting array.

const matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]];

const sumNotUnderZero = (bottom, top) => 
  top.map((v, i) => v ? v + bottom[i] : v);

const result = matrix.reduceRight(sumNotUnderZero)
  .reduce((s, n) => s + n);

console.log(result);
Ori Drori
  • 183,571
  • 29
  • 224
  • 209
1

You could use Array#reduceRight for building another array with valued column sums and then use Array#reduce for a single number.

var matrix = [[0, 1, 1, 2], [0, 5, 0, 0], [2, 0, 3, 3]],
    result = matrix.reduceRight(function (a, b) {
        return b.map(function (c, i) {
            return c && c + a[i];
        });
    }).reduce(function (a, b) {
        return a + b;
    });

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

Should be able to simplify it and use this:

function matrixElementsSum(matrix) {
    var Summa =0
    for (i = 0; i < matrix.length; i++)
      for(j = 0; j < matrix[i].length; j++)
         if (matrix[i-1][j] != 0)
           Summa = Summa + matrix[i][j];

    return Summa;
}

You need to access first the array above your current one, hence the matrix[i-1] and then the same column, hence the [j] in (matrix[i-1])[j] ~ matrix[i-1][j]

Easton Bornemeier
  • 1,918
  • 8
  • 22