Skip to content

Instantly share code, notes, and snippets.

@hassan-maavan
Created September 21, 2020 14:01
Show Gist options
  • Save hassan-maavan/594c0e7557a9df69f3d3440ad17d7bb2 to your computer and use it in GitHub Desktop.
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)
// [
// [ 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