Solving Popular Algorithms: Matrix Diagonal Sums

This is the wrong type of matrix
Example of Matrix Diagonals via LeetCode
mat = [[1,2,3],[4,5,6],[7,8,9]]
diagonalSum = (mat) => {
let primSum = 0

for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
}

}
diagonalSum = (mat) => {
let primSum = 0
let secSum = 0
for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
secSum += mat[i][mat.length-1-i]
}

}
diagonalSum = (mat) => {
let primSum = 0
let secSum = 0
for (let i = 0; i < mat.length; i++) {
primSum += mat[i][i]
secSum += mat[i][mat.length-1-i]
}

if (mat.length%2 !== 0) {
let mid = Math.floor(mat.length/2)
secSum -= mat[mid][mid]
}

return primSum + secSum
}
Did you know Diagon Alley put together is Diagonally?!?

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store