Check Whether Date Ranges Overlap
Write the code, see it run, pass the checks.
The problem
- medium
- Code
Export a DateRange type with read-only start and end dates, and dateRange(start, end), which throws if start is after end and otherwise returns the range; it's the only way to make one, so every DateRange is known to be in order. Then write overlaps(a, b), true when the ranges share at least one instant (touching ends count), and clamp(date, range), which returns the date moved into the range if it falls outside. Type it so a plain { start, end } object isn't accepted as a DateRange and a range's dates can't be reassigned.
Your code must pass
- Throws when start is after end
- Tells overlapping ranges apart
- Clamps a date into the range
- Has no type errors
- Rejects an unchecked object as a range
- A range's dates are read-only
- Exposes start and end as dates