hinagawa

Matrix's Diagonal Sum

Feb 6th, 2021 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let matrixExample = [
  2.     [1, 2, 3, 4, 6],
  3.     [4, 5, 6, 5, 6],
  4.     [7, 8, 9, 7, 5],
  5.     [7, 8, 9, 7, 3],
  6.     [7, 8, 9, 7, 3]
  7.  
  8. ];
  9.  
  10. function sumUpDiagonals(matrix) {
  11.     let n = matrix.length;
  12.     let mainSum = 0;
  13.     let secondarySum = 0;
  14.     for (let i = 0; i < n; i++) {
  15.         mainSum += matrix[i][i];
  16.         secondarySum += matrix[i][n - 1 - i];
  17.     }
  18.     return { mainSum, secondarySum };
  19. }
  20.  
  21. console.log(sumUpDiagonals(matrixExample));
  22.  
Add Comment
Please, Sign In to add comment