Skip to content

Instantly share code, notes, and snippets.

@headfire94
Created December 10, 2016 12:07
Show Gist options
  • Save headfire94/b94b678f4d313016298ffad4b45ec3ae to your computer and use it in GitHub Desktop.
Save headfire94/b94b678f4d313016298ffad4b45ec3ae to your computer and use it in GitHub Desktop.
Сумма основной и побочной диагонали матрицы
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