Issue 2 - Generate a range of timeslots

March 11, 2017

Question

Given a timeInterval, startTime and endTime, generate an array of timeslots.


/**
 * Generate an array of timeslots based on timeinterval
 * Assumption for brevity: 
 * - startTime and endTime are of valid time
 * - startTime will never be later than end time
 * 
 * @param  {integer} timeinterval   In mins. Can only accept 15, 30 or 60
 * @param  {string}  startTime      '03:45'. Min - '00:00', Max - '24:00'
 * @param  {string}  endTime        '15:00'. Min - '00:00', Max - '24:00'
 * @return {array}                  ['03:45', ....., '15:00']
 */

function generateTimeslots(timeInterval, startTime, endTime) {
  /** Your code here */
}

// Examples
generateTimeslots(5, '13:15', '18:45'); // Error: Can only accept 15, 30, 60
generateTimeslots(30, '13:15', '18:45'); // Error: Incorrect time interval
generateTimeslots(15, '13:15', '18:45'); // ['13:15', '13:30', '13:45', ....., '18:45']
generateTimeslots(30, '08:00', '14:00'); // ['08:00', '08:30', '09:00', ....., '13:30', '14:00']

Expected Output


// Solution must be able to pass the following test
generateTimeslots(5, '00:00', '24:00'); // Error: Can only accept 15, 30, 60
generateTimeslots(30, '08:45', '10:15'); // Error: Incorrect time interval
generateTimeslots(15, '08:45', '10:15'); // ['08:45', '09:00', '09:15', '09:30', '09:45', '10:00', '10:15']
generateTimeslots(15, '13:15', '15:00'); // ['13:15', '13:30', ...., '14:45','15:00']
generateTimeslots(30, '08:00', '14:00'); // ['08:00', '08:30', '09:00', ....., '13:30', '14:00']
generateTimeslots(60, '21:00', '24:00'); // ['21:00', '22:00', '23:00', '24:00']

Assumption

Extra note

I have received questions on why generateTimeslots(30, '08:45', '10:15') generates an error. The short answer is the the time interval should adds up perfectly to the hour mark. '08:45' plus 30 mins will exceed the hourly mark.

Long story

This function was part of a larger feature where time interval determines what type of timeslots can be generated. If a time interval is 15 mins, timeslots generated can only be in 15 mins segments such as 08:00, 08:15, 08:30, 08:45, ... etc. This applies for 30 mins, where timeslots can only be in 30 mins segments such as 08:00, 08:30, 09:00.


How To Submit Your Solution

Preferred Method

Solve the question in JSFiddle, JSBin or Codepen and save the link. Submit the link via email or paste it in the comment section below.

Cut-off date

We will stop evaluating submission after 31st March, 2017 23:59. The answers will then be consolidated and the best few solutions will be picked. The selected solutions will be updated on this post.

ES version

Any ES version are welcome.

Submissions

There were multiple submissions but ultimately there were only two submissions that checked all the requirements of the challenge. This is largely down to my fault as I did not the phrase the questions properly.

The biggest question mark is "What does this error: Error: Incorrect time interval means"?

On hindsight, I should have added a requirement that would have cleared up the confusion - Any timeslots generated must be perfectly divisible by the time interval.

Anywayyyyyyy, let's get to the two solutions:

  1. Class-y and elegant by attilab
    This is my pick for this challenge. The code was easy to digest and is way more elegant than what I came up. Great to see "The Constructor Pattern" in action here.

  2. Letting the Date object do the work by TommyGuns
    TommyGuns made the smart choice of letting the Date object do the weightlifting for time manipulation. However, there are several things that weren't so perfect such as :

    • the global scope pollution of the timeSlot variable
    • Error not thrown and catch properly: throw('incorrect interval!')
    • Time interval generated as a slightly wrong format - "14:0" instead of "14:00"

Do check out the rest of the submissions in the comments section. Great job, everyone! Til the next time..

RSS

Like solving mini javascript questions? Subscribe below for more!

Subscribe to my newsletter on Modern Web Development

Weekly delivery of web goodies - Code Tips, 5CWS and Blog Updates