0

I was working on an algorithm on hackerrank and was wondering why my answer was not correct.

Here is the function

function diagonalDifference(arr) {
// Write your code here

let first = 0
let second = 0
let endArr = arr.length-1
for(let i=0;i<arr.length;i++){
    first += arr[i][i]
    second += arr[i][endArr]
    console.log(endArr)
    endArr = endArr--
}


let result = Math.abs(first-second)
console.log(result)
return result

}

it was not until I changed the statement endArr = endArr-- to endArr = endArr-1 that I got my desired answer.

I would let to know why. I thought -- meant decrementing by 1?

zztopz
  • 33
  • 3

0 Answers0