I am working on a queue system for the clinic. The person at the front desk has two options for adding patients to the queue.
- Patients with fixed appointments
- Pleasure Patients
So, for example, there are already four patients in the queue, my existing array of appointments looks like
existing_appointments = ["09:30", "10:00", "12:15", "13:45"];
And the average scan time for a patient is 15 minutes.
avg_wait_per_patient = 15;
As soon as the patient enters, I will find for him the best available time interval.
Say it is now 09:00
current_time = "09:00";
The following function find_free_slot()does not work, because it returns 09:15 instead of 09:00, since there is no destination in this slot.
, current_time + avg_wait_per_patient, current_time. , , , . , last_index_of_array + avg_wait.
function toMinutes(t) {
return 60 * Number(t.split(":")[0]) + Number(t.split(":")[1]);
}
function reverse_toMinutes(t) {
return ("0" + Math.floor(t / 60)).slice(-2) + ":" + ("0" + t % 60).slice(-2);
}
function find_free_slot(ct,appointments,avg_wait) {
ct = toMinutes(ct);
free_slot = '';
if(appointments.length==0) {
free_slot = ct;
} else {
for(i=0; i<appointments.length; i++) {
appointment = toMinutes(appointments[i]);
if(free_slot <= appointment - avg_wait) {
i == 0 ?
free_slot = ct + avg_wait :
free_slot = toMinutes(appointments[i - 1]) + avg_wait;
break;
}
}
}
return reverse_toMinutes(free_slot);
}
jsfiddle