javascript - 在时间数组中查找最近的空闲槽

标签 javascript arrays

我正在为一家诊所开发一个排队系统。前台人员有两个选项可以将患者添加到队列中。

  1. 有固定预约的患者
  2. 门诊病人

所以例如队列中已经有四个病人,我现有的预约数组看起来像

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));

关于javascript - 在时间数组中查找最近的空闲槽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42292095/

相关文章:

返回错误维度的 Javascript 多维数组

arrays - Swift 字典到数组来填充 pickerView

javascript - 数组元素的增量幂

javascript - 仅使用 for 循环来迭代对象数组与其他数组

c# - 有效地将 IEnumerable 值折叠在一起

javascript - 我想使用 JavaScript 从路径中删除文件名

javascript - Ajax (jQuery) PHP 脚本//move_uploaded_file-问题

javascript - javascript 如何对具有相同属性的数组进行排序?

javascript - 使用作为 URL 参数传递的 jQuery 选择器 - 安全吗?

java - 使用数组查找两个整数的倍数