Created
December 18, 2016 10:49
-
-
Save Roughtron/673ddd6417ac43d0953765ca066cf8a8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var matrixExample = [ | |
| [ 1, 2, 3, 4 ], | |
| [ 4, 5, 6, 5 ], | |
| [ 7, 8, 9, 7 ], | |
| [ 7, 8, 9, 7 ] | |
| ]; | |
| function sumMainDiagonal(matrix) { | |
| var sum = 0; | |
| for (var i = 0; i < matrix.length; i++) { | |
| sum += matrix[i][i]; | |
| } | |
| return sum; | |
| } | |
| function sumSecondaryDiagonal(matrix) { | |
| var sum = 0; | |
| for (var i = 0; i < matrix.length; i++) { | |
| sum += matrix[i][matrix.length - i - 1]; | |
| } | |
| return sum; | |
| } | |
| function sumUpDiagonals(matrix) { | |
| return sumMainDiagonal(matrix) + sumSecondaryDiagonal(matrix); | |
| } | |
| console.log(sumUpDiagonals(matrixExample)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment