I tried to do it but i am not able put condition. I want to find common slot.
- first person test is available between 10 AM to 12PM
- second person elton is available between 9 AM to 10:30 AM and 10:30
AM to 11 AM - third person shawn is available between 10:30 AM to 11:00 AM
Hence common slot will be 10:30 AM to 11:00 AM
const input = [
{
email: '[email protected]',
range: [
{start: '10:00:00', end: '12:00:00' }
],
},
{
email: '[email protected]',
range: [
{start: '09:00:00', end: '10:30:00'},
{start: '10:30:00', end: '11:00:00' }
],
},
{
email: '[email protected]',
range: [
{start: '10:30:00', end: '11:00:00' }
],
},
];
output:
[
{start: '10:30:00', end: '11:00:00' }
]
second input example
const input = [
{
email: '[email protected]',
range: [
{start: '10:00:00', end: '12:00:00' }
],
},
{
email: '[email protected]',
range: [
{start: '09:00:00', end: '10:30:00'},
{start: '10:30:00', end: '11:00:00' },
{start: '14:30:00', end: '15:00:00' }
],
}
];
output:
[
{start: '10:00:00', end: '11:00:00' }
]
third input example:
const input = [
{
email: '[email protected]',
range: [
{start: '10:00:00', end: '12:00:00' },
{start: '14:30:00', end: '15:00:00' }
],
},
{
email: '[email protected]',
range: [
{start: '09:00:00', end: '10:30:00'},
{start: '10:30:00', end: '11:00:00' },
{start: '14:30:00', end: '15:00:00' }
],
}
];
output :
[
{start: '10:00:00', end: '11:00:00' },
{start: '14:30:00', end: '15:00:00' }
]
Code I have tried so far
function generateIntersectSlot(interviewerRange) {
if (!interviewerRange.length) {
return [];
}
for (let interviewer of interviewerRange) {
for (let nextInterviewer of interviewerRange) {
if (interviewer.email == nextInterviewer.email) {
continue;
}
console.log(interviewer.email, 'interviewerSlot email')
console.log(nextInterviewer.email, 'nextInterviewer email')
const interviewerSlot = interviewer.range;
const nextInterviewerSlot = nextInterviewer.range;
for (let intSlot of interviewerSlot) {
for (let nextIntSlot of nextInterviewerSlot) {
console.log(intSlot, 'intSlot')
console.log(nextIntSlot, 'nextIntSlot')
// if(new Date(`${getDate}T${intSlot.start}`) >= new Date(`${getDate}T${nextIntSlot.start}`) ) {
// console.log(intSlot, 'asd')
// } else {
// }
}
}
}
}
}
generateIntersectSlot(input);
<script>
const input = [{
email: '[email protected]',
range: [{
start: '10:00:00',
end: '12:00:00'
}],
},
{
email: '[email protected]',
range: [{
start: '09:00:00',
end: '10:30:00'
},
{
start: '10:30:00',
end: '11:00:00'
}
],
},
{
email: '[email protected]',
range: [{
start: '10:30:00',
end: '11:00:00'
}],
},
];
</script>
3
Answers
try this one
Find a common ranges for the two first interviewers, than compare them to the ranges of the next interviewer and repeat to the last interviewer:
We need to implement the function to intersect and union ranges and then we will need to loop the persons and ranges, find the intersections and then merge intersections that belong together: