We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent dab33ab commit a15de9eCopy full SHA for a15de9e
javascript/1572-matrix-diagonal-sum.js
@@ -0,0 +1,15 @@
1
+/**
2
+ * @param {number[][]} mat
3
+ * @return {number}
4
+ */
5
+var diagonalSum = function (mat) {
6
+ let sum = 0; // initialize sum to zero
7
+ let n = mat.length - 1; // initialize n to mat length - 1
8
+ for (let i = 0; i <= n; i++) { // loop through to 0 to n
9
+ sum += mat[i][i]; // add mat[i][i] to sum
10
+ if (i !== (n - i)) { // if i not equal to n - i then add mat[i][n - i] to sum
11
+ sum += mat[i][n - i];
12
+ }
13
14
+ return sum; // return sum;
15
+};
0 commit comments