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