Created
September 21, 2020 14:01
-
-
Save hassan-maavan/594c0e7557a9df69f3d3440ad17d7bb2 to your computer and use it in GitHub Desktop.
Create a function that receives a (square) matrix and calculates the sum of both diagonals (main and secondary)
This file contains 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
// [ | |
// [ 1, 2, 3 ], | |
// [ 4, 5, 6 ], | |
// [ 7, 8, 9 ] | |
// ] | |
function sum(matrix) { | |
return matrix.reduce((sum, value, index) => sum + value[index] + value[value.length - index -1], 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment