Asia Thomas

Given a square matrix, calculate the absolute difference between the sums of its diagonals.

For example, the square matrix is shown below:

1 2 3
4 5 6
9 8 9

The left-to-right diagonal = 1+5+9=15 . The right to left diagonal = 3+5+9=17. Their absolute difference is |15–17|=2.

function diagonalDifference(arr) {
let idx = arr.length - 1
let rightTotal = 0
let leftTotal = 0
for (let i = 0; i < arr.length; i++) {
rightTotal += arr[i][i]
leftTotal += arr[i][idx]
idx--
}
return Math.abs(rightTotal - leftTotal)
}

--

--

Given a time in -hour AM/PM format, convert it to military (24-hour) time.

Note: — 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
- 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

Example

  • s = ‘12:01:00PM.
  • Return ‘12:01:00’.
  • s = ‘12:01:00AM’.
  • Return ‘00:01:00’.
function timeConversion(s) {    let day = s.slice(8) // pm or am
let time = s.slice(0, 2) // 12
let middle = s.slice(2, -2)
if(day === 'PM'){
if(time === '12'){
return `${time}${middle}`
} else {
time = Number(time)
let newTime = time += 12
return `${newTime}${middle}`
}
} else {
if(time === '12'){
return `00${middle}`
} else {
return `${time}${middle}`
}
}
}

--

--