[JavaScript] Simple solution using two pointers with explanation | O(n)
I\'ve seen many solutions using extra variables to keep track of rows and columns but since we are going in diagonal we can assume that coordinates for each cel
I\'ve seen many solutions using extra variables to keep track of rows and columns but since we are going in diagonal we can assume that coordinates for each cell in the matrix for our first diagonal line will be:\n```\nx = i + 1\ny = i + 1\n```\n\nAs for the other one, we will just go in the opposite direction and decrement by one starting from the last element in each row\nIf the indices collide we just skip the increment so we don\'t need to subtract later\n\n```javascript\nconst diagonalSum = (mat) => {\n  let sum = 0\n  let j = mat[0].length - 1\n\n  for (let i = 0; i < mat.length; i++, j--) {\n    if (i !== j)\n      sum += mat[i][j]\n    sum += mat[i][i]\n  }\n\n  return sum\n}\n```\n\nVersion with `forEach` (a bit easier to read):\n```javascript\nconst diagonalSum = (mat) => {\n  let sum = 0\n  let j = mat[0].length - 1\n\n  mat.forEach((row, i) => {\n    if(i !== j - i)\n      sum += row[j - i]\n    sum += row[i]\n  })\n\n  return sum\n}\n```
['Two Pointers', 'JavaScript']