Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let matrixExample = [
- [1, 2, 3, 4, 6],
- [4, 5, 6, 5, 6],
- [7, 8, 9, 7, 5],
- [7, 8, 9, 7, 3],
- [7, 8, 9, 7, 3]
- ];
- function sumUpDiagonals(matrix) {
- let n = matrix.length;
- let mainSum = 0;
- let secondarySum = 0;
- for (let i = 0; i < n; i++) {
- mainSum += matrix[i][i];
- secondarySum += matrix[i][n - 1 - i];
- }
- return { mainSum, secondarySum };
- }
- console.log(sumUpDiagonals(matrixExample));
Add Comment
Please, Sign In to add comment