Drill

ProblemsJavaScript › events

How long is this event in minutes

easyeventsJavaScript

An event start and end are given as four-digit HHMM integers. Return the duration in minutes.

eventLengthMins(startHhmm: int, endHhmm: int) → int

Solve it in the editor →

Where you start

function eventLengthMins(startHhmm, endHhmm) {
  
}

Worked examples

CallResult
eventLengthMins(930, 1130)120
eventLengthMins(0, 100)60
eventLengthMins(2300, 100)120
eventLengthMins(1200, 1200)0

Hint

Convert each to minutes (hh × 60 + mm); the answer is the difference mod 1440, so add 1440 when it comes out negative.

Reference solution in JavaScript
function eventLengthMins(startHhmm, endHhmm) {
  const sMin = Math.floor(startHhmm / 100) * 60 + (startHhmm % 100);
  const eMin = Math.floor(endHhmm / 100) * 60 + (endHhmm % 100);
  const diff = eMin - sMin;
  return ((diff % 1440) + 1440) % 1440;
}

The same problem in another language

More events problems in JavaScript