简体   繁体   中英

How to split time slots evenly between vendors, with remainders

I have a massage booking app where-by users can book multiple sessions with a length in minutes. The session length can go between 30 minutes to 2 hours but are in slots of 30 minutes.

Vendor = Someone giving massages.
Client = Someone receiving them

Each vendor cannot have more than 4 clients each. The client can book multiple people with multiple vendors.

They can book the length of the session in minutes per person, the number of people, and the number of vendors.

Now my question is, how can I split the 30-minute time slots between each of the vendors evenly depending on the length per person and the number of people? I tried to have a go myself but cannot figure this out. Below is my attempt. Each vendor can have up to 4 hours in 30-minute slots. There are some examples inside the code.

I created a JS fiddle, please access it here

And here is an example of the code and my attempt that only partially works.

// Examples

// WORKS
// 3 vendors have 2 hours. 1 has 3 hours
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 7
const numberOfVendors = 3 */

// WORKS
// 3 vendors have 2 hours.
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 6
const numberOfVendors = 3 */

// WORKS
// 2 vendors have 2 hours, 1 has 1 hour.
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 5
const numberOfVendors = 3 */

// WORKS
// 1 vendor has 4 hours
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 4
const numberOfVendors = 1 */

// WORKS
// 3 vendor has 1 hours. One has 2
// const lengthInMinutes = 60
// const hours = lengthInMinutes / 60;
/* const numberOfClients = 4
const numberOfVendors = 3 */

// BROKEN
// 4 vendors have 2 hours, 2 has 1 hour
const lengthInMinutes = 60
const hours = lengthInMinutes / 60;
const numberOfClients = 10
const numberOfVendors = 6


let totalHours = numberOfClients * hours;
console.log("totalHours: " + totalHours)

let hoursPerVendor = Math.round(totalHours / numberOfVendors);
let hoursRemainder = (totalHours / numberOfVendors) % 1;

let vendorHours = new Array(numberOfVendors).fill(0).map((i) => {
    return hoursPerVendor
})

console.log("Hours remainder: " + hoursRemainder)
if (hoursRemainder !== 0.0) {
  if (hoursRemainder > 0.5) {
    // Take one hour off.
    vendorHours[vendorHours.length - 1] = vendorHours[vendorHours.length - 1] - 1
  }
  if (hoursRemainder < 0.5) {
    // Add one hour on.
    vendorHours[vendorHours.length - 1] = vendorHours[vendorHours.length - 1] + 1
  }
}

console.log(vendorHours)

I figured it out for anyone else stuck.

const lengthInMinutes = 60
const numberOfClients = 2
const numberOfVendors = 2

const totalMinutes = lengthInMinutes * numberOfClients
console.log("TotalMinutes: " + totalMinutes)

const totalSlots = ((totalMinutes / 30));
console.log("TotalSlots: " + totalSlots)

const slotsPerVendor = totalSlots / numberOfVendors
console.log("SlotsPerVendor: " + slotsPerVendor)

let currentVendorIndex = 0;
let vendors = new Array(numberOfVendors).fill(0).map((i) => i)

// Loop over slots and add them to each vendor
new Array(totalSlots).fill(0).map((i) => {
    if (currentVendorIndex > vendors.length - 1) {
    currentVendorIndex = 0;
  }
  if (vendors[currentVendorIndex] < 240) {
    vendors[currentVendorIndex] = vendors[currentVendorIndex] + 30
    currentVendorIndex++;
  }
})

console.log(vendors)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM