Created
February 25, 2022 07:36
-
-
Save blessedjasonmwanza/b35271071580920c1efcf9581d5d15e9 to your computer and use it in GitHub Desktop.
HackerRank Diagonal difference challenge solution JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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