Adee
Adee

Reputation: 377

Check if a time range is not within other time ranges?

I need to check if the given time range is available or not.

Array with busy hours:

[
  { from: 2, to: 4 },
  { from: 8, to: 10 }
]

The compartment I want to check

{ from: 3, to: 6 }

Expected result:

{ from: 1, to: 2 } // true
{ from: 5, to: 7 } // true
{ from: 3, to: 4 } // false
{ from: 9, to: 10 } // false

Upvotes: 1

Views: 367

Answers (1)

Maheer Ali
Maheer Ali

Reputation: 36574

You can use some()

const arr = [
  { from: 2, to: 4 },
  { from: 8, to: 10 }
]

function checkTime(arr,obj){
  return !arr.some(x => x.from <= obj.from && x.to >= obj.to);
}

let tests = [
  { from: 1, to: 2 },
  { from: 5, to: 7 }, 
  { from: 3, to: 4 },
  { from: 9, to: 10 }
]
tests.forEach(x => console.log(checkTime(arr,x)));

Upvotes: 3

Related Questions