Created
December 10, 2016 10:35
-
-
Save Erushenko/308b4ab9dfd0bdfae12e72ccc710376a to your computer and use it in GitHub Desktop.
Get the sum diagonals in the matrix on javascript
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 sumUpDiagonals(matrix) { | |
var sumDiagonals = {main: 0, second: 0}, | |
matrixLength = matrix.length; | |
for (var i = 0; i < matrixLength; i++) { | |
sumDiagonals.main += matrix[i][i]; | |
sumDiagonals.second += matrix[i][matrixLength-i-1]; | |
} | |
return sumDiagonals | |
} | |
console.log(sumUpDiagonals(matrixExample)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hope this helps @calvinalee2006