Skip to content

Instantly share code, notes, and snippets.

@leo-bianchi
Last active April 5, 2020 05:14
Show Gist options
  • Save leo-bianchi/a47538748f9a26c11df89c986b8d0a52 to your computer and use it in GitHub Desktop.
Save leo-bianchi/a47538748f9a26c11df89c986b8d0a52 to your computer and use it in GitHub Desktop.
Diagonal Difference (HackerRank)
// 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