Skip to content

Instantly share code, notes, and snippets.

@chulanovskyi
Created December 11, 2016 07:55
Show Gist options
  • Save chulanovskyi/da4db31f8e89aae2b82cf9d611c08202 to your computer and use it in GitHub Desktop.
Save chulanovskyi/da4db31f8e89aae2b82cf9d611c08202 to your computer and use it in GitHub Desktop.
Разработайте функцию sumUpDiagonals() https://gist.github.com/alekseyr/ad1dfd166bc4e5522da16f22bed5bc7e
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