Skip to content

Instantly share code, notes, and snippets.

@Drugak
Created January 12, 2016 13:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Drugak/1f85546f189b0e425ac3 to your computer and use it in GitHub Desktop.
Save Drugak/1f85546f189b0e425ac3 to your computer and use it in GitHub Desktop.
Given a square matrix of size N×N, calculate the absolute difference between the sums of its diagonals.
var a = [
[1,2,3,5],
[1,5,9,5],
[1,5,0,8],
[9,5,0,5]
]
function diagonalsX() {
var result = 0;
a.forEach(function(item , i ,a) {
result = result + item[i];
})
return result;
}
function diagonalsY() {
var result = 0;
a.forEach(function(item , i ,a) {
result = result + item[item.length - (i+1)];
})
return result;
}
console.log(Math.abs(diagonalsX() - diagonalsY()));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment