Last active
April 5, 2020 05:14
-
-
Save leo-bianchi/a47538748f9a26c11df89c986b8d0a52 to your computer and use it in GitHub Desktop.
Diagonal Difference (HackerRank)
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
// Without reduce | |
function diagonalDifference(arr) { | |
let sum = 0; | |
for (let i = 0; i <= arr.length - 1; i++) { | |
sum += arr[i][i] - arr[i].reverse()[i]; | |
} | |
return Math.abs(sum); | |
} | |
// With reduce | |
function diagonalDifference(arr) { | |
return Math.abs(arr.reduce((acc, array, i) => acc += array[i] - array.reverse()[i], 0)); | |
} | |
// Verify square matrix | |
const matrix = Array(4).fill(Array(4).fill(0)); | |
function isSquare(elem, index, array) { | |
return elem.length == array.length; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment