-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyCalendar.js
More file actions
34 lines (31 loc) · 787 Bytes
/
MyCalendar.js
File metadata and controls
34 lines (31 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
function MyCalendar() {
this.calArr = [];
this.bookedArr = [];
}
// var calArr = [{
// mLeft: 10,
// mRight: 40,
// },
// {
// mLeft: 50,
// mRight: 60,
// }];
// var bookedArr = [];
// }
MyCalendar.prototype.book = function(left, right) {
for(var i = 0 ; i < this.bookedArr.length; i++) {
if (this.bookedArr[i].mLeft < right && left < this.bookedArr[i].mRight) {
return false;
}
}
for(var j = 0; j < this.calArr.length; j++) {
if(this.calArr[j].mLeft < right && left < this.calArr[j].mRight) {
this.bookedArr.push({
mLeft: Math.max(left, this.calArr[j].mLeft),
mRight: Math.min(right, this.calArr[j].mRight)
})
}
}
this.calArr.push({ mLeft: left, mRight: right });
return true;
};