0

problem statement

i need to push new appointment inside this array but, i also DONOT want to overlap time of other already booked appointment

my array

Booked appointment contains previously added/booked appointments!

Allappointments= [ { "startDay": 0, "endDay": 7, "Booked Appointments": [ { "startTime": 1530, "endTime": 1700 }, { "startTime": 1030, "endTime": 1300 } ] } ];

half solution

i have managed to get this solution so far which which which works for start and end time, But doesnt cover the start and end Day part, day start with 0 and 7, covering whole week!

function validateTimeFrame(newTimeFrame, existingTimeFrames) { const { startTime, endTime } = newTimeFrame;

    // Check for intersection with existing time frames
    const hasIntersection = existingTimeFrames.some((timeFrame) =>
      (startTime >= timeFrame.startTime && startTime <= timeFrame.endTime) ||
      (endTime >= timeFrame.startTime && endTime <= timeFrame.endTime)
    );
  
    // Check if it is encompassed by another time frame
    const isEncompassed = existingTimeFrames.some((timeFrame) =>
      startTime >= timeFrame.startTime && endTime <= timeFrame.endTime
    );
  
    // Check if it encompasses another time frame
    const encompasses = existingTimeFrames.some((timeFrame) =>
      startTime <= timeFrame.startTime && endTime >= timeFrame.endTime
    );
  
    return {
      hasIntersection,
      isEncompassed,
      encompasses
    };
  }
  
  // Example usage:
  const existingTimeFrames = [
    { startTime: 9, endTime: 10 },
    { startTime: 12, endTime: 14 },
    { startTime: 16, endTime: 18 }
  ];
  
  const newTimeFrame = { startTime: 11, endTime: 13 };
  
  const validationResult = validateTimeFrame(newTimeFrame, existingTimeFrames);
  console.log(validationResult);
  

1 Answer 1

0

A day = 24 hours so it's enough to solve it just for hours. Testing for overlap between 2 ranges is easily searchable in the web. So I constructed it into a written idea. First doing it for ranges of hours.

function is_intersect(range1, range2) {
  var x1 = range1.startTime
  var x2 = range1.endTime
  var y1 = range2.startTime
  var y2 = range2.endTime
  return x1 <= y2 && y1 <= x2
}

function can_squeeze_in (range, ranges) {
  return !(ranges.find((item) => is_intersect(range, item)))
}

const existingTimeFrames = [
  { startTime: 9, endTime: 10 },
  { startTime: 12, endTime: 14 },
  { startTime: 16, endTime: 18 }
];
  
const newTimeFrame = { startTime: 11, endTime: 13 };
const willFit = {startTime: 19, endTime: 100}

console.log(can_squeeze_in(newTimeFrame, existingTimeFrames))
console.log(can_squeeze_in(willFit, existingTimeFrames))

Now if you have also startDay and endDay for each range, you can multiply them by 24 and add the startTime or endTime accordingly to reduce the problem.

var range1 = {
  startDay: 2, startTime: 12,
  endDay: 3, endTime: 18
}


function normalize_range(range) {
  return {
    startTime: range.startDay * 24 + range.startTime,
    endTime: range.endDay * 24 + range.endTime,
  }
}


console.log(normalize_range(range1))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.