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

Popular posts from this blog

minimum and maximum values that can be calculated

TimeConversion in 24 hour format

calculate the ratios of its elements that are positive, negative, and zero. Print the decimal value of each fraction on a new line with places after the decimal.