css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              /**
 * 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) {
      
 var validateBookings = function() {
      //Create a date Object using the supplied timeInterval
      var dateInterval = new Date();
          dateInterval.setHours(00)
          dateInterval.setMinutes(timeInterval);
          dateInterval.setSeconds(00);

        timeSlot = dateInterval;
  }

  //
  var rejectBookings = function() {
    throw('incorrect interval!')        
  }
  
  //Acceptable time ranges in an array. Validate the supplied interval.
  var timeRange = [15,30,60]; 
  timeRange.includes(timeInterval) ? validateBookings() : rejectBookings();
  
  //Convert start and end strings into date Objects.
  var splitStart = startTime.split(":")
  var splitEnd = endTime.split(":")
  
  var startHours = splitStart[0];
  var startMins = splitStart[1]
  
  var endHours = splitEnd[0];
  var endMins = splitEnd[1]
  

  //Create Start Time date object
  var startDate = new Date();
  startDate.setHours(startHours);
  startDate.setMinutes(startMins);
  startDate.setSeconds('00');
  //Create End Time date object
  var endDate = new Date();
  endDate.setHours(endHours);
  endDate.setMinutes(endMins);
  endDate.setSeconds('00');

//Set the starting point for the time slots
    timeSlot.setHours(startDate.getHours());
    timeSlot.setMinutes(startDate.getMinutes());

//Set the array to hold the slots
    var finalSlots = [];




//This function validates that the timeslots are all within an acceptable range and fall within the hour.
  var checkHours = function(hours) {

    var validateArray = [];
      //Check to see if the time range is valid (must fall on the hour.
      for (i = 0; i <= hours.length -1; i++) {
          if (hours[i].substr(hours[i].indexOf(":") + 1) != 00 || hours[i].substr(hours[i].indexOf(":") + 1) != 0)
          {
              validateArray.push('fail');
          }
          else {
              validateArray.push('pass');
          }
      }
      return validateArray;
  }




//Calculate the time slots between the start and end timings
  var incrementTimeSlots = function() {
    //Simple loop that pushes the time intervals to an array.
    while((endDate.getTime() + timeInterval) > timeSlot.getTime()) {
        var interval = timeSlot.getHours() + ':' + timeSlot.getMinutes();
        finalSlots.push(interval);
        //increment the timeslot & keep the loop going.
        timeSlot.setMinutes(timeSlot.getMinutes() + timeInterval)
    };
    //Check to see if at least one interval falls on the hour
    if (!checkHours(finalSlots).includes('pass')) {
        return('Error: Incorrect time Interval');
    }
    else {
        return finalSlots;
    }
  }
  return incrementTimeSlots();
}



//Output
// console.log(generateTimeslots(5, '00:00', '24:00')); // Error: Can only accept 15, 30, 60
console.log(generateTimeslots(30, '08:45', '10:15')); // Error: Incorrect time interval
console.log(generateTimeslots(15, '08:45', '10:15')); // ['08:45', '09:00', '09:15', '09:30', '09:45', '10:00', '10:15']
console.log(generateTimeslots(15, '13:15', '15:00')); // ['13:15', '13:30', ...., '14:45','15:00']
console.log(generateTimeslots(30, '08:00', '14:00')); // ['08:00', '08:30', '09:00', ....., '13:30', '14:00']
console.log(generateTimeslots(60, '21:00', '24:00')); 
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console