Problems › JavaScript › events
How long is this event in minutes
An event start and end are given as four-digit HHMM integers. Return the duration in minutes.
- 0930 means 9 hours and 30 minutes = 570 minutes.
- 2330 means 23 hours and 30 minutes = 1410 minutes.
- 2400 is a valid spelling of 24:00 = 1440 minutes.
- The duration is (end − start) minutes taken modulo 1440, so the result is always in 0..1439.
- Start and end at the same time gives 0.
eventLengthMins(startHhmm: int, endHhmm: int) → int
Where you start
function eventLengthMins(startHhmm, endHhmm) {
}
Worked examples
| Call | Result |
|---|---|
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;
}