Skip to content

Instantly share code, notes, and snippets.

@blessedjasonmwanza
Created February 25, 2022 07:36
Show Gist options
  • Save blessedjasonmwanza/b35271071580920c1efcf9581d5d15e9 to your computer and use it in GitHub Desktop.
Save blessedjasonmwanza/b35271071580920c1efcf9581d5d15e9 to your computer and use it in GitHub Desktop.
HackerRank Diagonal difference challenge solution JS
// Given a square matrix, calculate the absolute difference between the sums of its diagonals.
function diagonalDifference(arr) {
// Write your code here
let right = 0;
let left = 0;
let interations = arr.length -1;
for(let i = 0; i < arr.length; i++){
right += arr[i][i];
left += arr[i][interations];
interations -=1;
};
return Math.abs(right - left);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment