Last active
January 9, 2017 18:26
-
-
Save KarafiziArtur/d2dbb72ed2fdadf10283717939192293 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const arr = [ | |
[1,2,3,4], | |
[4,5,6,5], | |
[7,8,9,6], | |
[1,4,6,2] | |
]; | |
const sumUpDiagonals = arr => { | |
let sumOfFirstDiagonal = 0; | |
let sumOfSecondDiagonal = 0; | |
let sumOfBothDiagonals = 0; | |
const lengthOfArray = arr.length; | |
for (let i = 0; i < lengthOfArray; i++) { | |
sumOfFirstDiagonal += arr[i][i]; | |
} | |
for (let i = lengthOfArray; i > 0; i--) { | |
sumOfSecondDiagonal += arr[i - 1][lengthOfArray - i]; | |
} | |
sumOfBothDiagonals = sumOfFirstDiagonal + sumOfSecondDiagonal; | |
return sumOfBothDiagonals; | |
} | |
console.log(sumUpDiagonals(arr)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment