Created
December 11, 2016 07:55
-
-
Save chulanovskyi/da4db31f8e89aae2b82cf9d611c08202 to your computer and use it in GitHub Desktop.
Разработайте функцию sumUpDiagonals() https://gist.github.com/alekseyr/ad1dfd166bc4e5522da16f22bed5bc7e
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) { | |
var diagonals = []; | |
for (var i=0; i < matrix.length; i++){ | |
diagonals.push(matrix[i][i]); | |
diagonals.push(matrix[i][matrix.length-i-1]); | |
} | |
return diagonals.reduce(function (x, y){ | |
return x+y; | |
}); | |
} | |
console.log(sumUpDiagonals(matrixExample)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment