javascript – 在时间数组中查找最近的免费插槽

我正在为诊所的队列系统工作.前台的人有两种方法可以将患者添加到队列中.

>具有固定预约的患者
>步入式病人

因此,例如队列中已有四名患者,我的现有约会数组看起来像

existing_appointments = ["09:30", "10:00", "12:15", "13:45"];

患者的平均检查时间为15分钟.

avg_wait_per_patient = 15;

一旦病人走了进去,我就找到了他最好的时间段.

说现在的时间是09:00

current_time = "09:00";

以下函数find_free_slot()不起作用,因为它返回09:15而不是09:00,因为此插槽上没有约会.

我的目标是,如果在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

最佳答案 问题在于:

i == 0 ?
    free_slot = ct + avg_wait :
    free_slot = toMinutes(appointments[i - 1]) + avg_wait;

如果您正在检查第一个约会(9:30)和免费时段< =(9:30 – 15),那么您将返回ct avg_wait,即9:00 15. 我重新修改了逻辑以使其工作:

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 = ct;   // The first slot you want to check is "right now"

  if (appointments.length == 0)
    return reverse_toMinutes(ct);

  for (i = 0; i < appointments.length; i++) {
    appointment = toMinutes(appointments[i]);
    if (ct <= appointment + avg_wait) {        // The appointment must be later than the current appointment's end time.
      if (free_slot <= appointment - avg_wait) // Free slot is enough time before the current appointment
        return reverse_toMinutes(free_slot);   // Return the free slot

      free_slot = toMinutes(appointments[i]) + avg_wait; // Otherwise, set the free slot to `avg` after the current appointment, to check the next iteration of the loop.
    }
  }
  return reverse_toMinutes(free_slot); // No free slot has been found, `free_slot` is `last appointment + avg`
}

var appointments = ["09:30", "10:00", "12:15", "13:45"];

console.log(" time - appointment");
console.log(" 9:00 -", find_free_slot("9:00", appointments, 15));
console.log(" 9:15 -", find_free_slot("9:15", appointments, 15));
console.log(" 9:16 -", find_free_slot("9:16", appointments, 15));
console.log(" 9:31 -", find_free_slot("9:31", appointments, 15));
console.log("10:09 -", find_free_slot("10:09", appointments, 15));
console.log("11:59 -", find_free_slot("11:59", appointments, 15));
console.log("12:00 -", find_free_slot("12:00", appointments, 15));
console.log("12:01 -", find_free_slot("12:01", appointments, 15));
点赞