Created
December 10, 2016 12:07
-
-
Save headfire94/b94b678f4d313016298ffad4b45ec3ae to your computer and use it in GitHub Desktop.
Сумма основной и побочной диагонали матрицы
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
var matrixExample = [ | |
[ 1, 2, 3, 4 ], | |
[ 4, 5, 6, 5 ], | |
[ 7, 8, 9, 7 ], | |
[ 7, 8, 9, 7 ] | |
]; | |
function sumUpDiagonals(matrix) { | |
return matrix.reduce(function(prev,current,index,arr) { | |
return prev + current[index] + current[arr.length-1-index] | |
}, 0); | |
} | |
console.log(sumUpDiagonals(matrixExample)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment