к сожалению из нее ничего не понял
const sumDiagonals = matrix =>
(matrix.length > matrix[0].length
? matrix[0]
: matrix
).reduce((acc, n, i) => {
acc.principal += matrix[i][i];
acc.secondary += matrix[i][matrix[i].length - i - 1];
return acc;
}, {
principal: 0,
secondary: 0,
});
const sumDiagonals = matrix =>
matrix.reduce((acc, n, i) => {
acc.principal += n[i] ?? 0;
acc.secondary += n[n.length - i - 1] ?? 0;
return acc;
}, {
principal: 0,
secondary: 0,
});