DEV Community

Cover image for Code Kata: Opening Hours
George WL
George WL

Posted on

Code Kata: Opening Hours

I was recently working on something for a client where they were going to provide opening and closing hours for each day of the week, and we were going to parse that in our UI.

The client decided to simplify the requirements, but I still think it would be interesting to see some people's solutions to the following challenge.

The Challenge

Given an object with the same structure as this example:

{
 Monday:{open: '09:00', close: '17:00'},
 Tuesday:{open: '09:00', close:'17:00'},
 Wednesday:{open: '09:00', close:'15:00'},
 Thursday:{open: '09:00', close:'17:00'},
 Friday:{open: '09:00', close:'17:00'},
 Saturday:{open: '10:00', close:'16:00'},
 Sunday:{},
}
Enter fullscreen mode Exit fullscreen mode

Return a new array of strings which groups continuous days with same hours together, and keeps the rest seperate.

If a day or group of days has no hours, return the day(s) followed by Closed

In the case of the above example, it would return the following

[
 'Monday-Tuesday: 09:00-17:00'
 'Wednesday: 09:00-15:00'
 'Thursday-Friday':'09:00-17:00'
 'Saturday: 10:00-16:00'
 'Sunday: Closed'
]
Enter fullscreen mode Exit fullscreen mode

Challenge Criteria:

  • Can work with any dates and times as long as they're in the expected format.
  • Use whatever programming language you feel comfortable with

I'm going to have a play around with this challenge myself and will post how I went about it in the comments, feel free to respond with how you did it.

Top comments (1)

Collapse
 
georgewl profile image
George WL

A consideration I didn't put is data validation, but let's just assume that there's always a open if there is a closing, and vice versa