Created
January 12, 2016 13:35
-
-
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.
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
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