calculate the absolute difference between the sums of its diagonals.
Given a square matrix, calculate the absolute difference between the sums of its diagonals.
For example, the square matrix is shown below:
function diagonalDifference(arr) {
let arrayOne= 0
let arrayTwo= 0
for(let i=0;i<arr.length;i++){
const k =(arr.length-1-i)
const array1 = arr[i][i]
const array2 = arr[i][k]
arrayOne+=array1
arrayTwo+=array2
}
if(arrayOne - arrayTwo < 0){
return Number((arrayOne - arrayTwo) * -1 )
}else{
return ((arrayOne - arrayTwo) )
}
}
Comments
Post a Comment