-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (74 loc) · 2.3 KB
/
Copy pathindex.js
File metadata and controls
80 lines (74 loc) · 2.3 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
const exampleRequests = [
{
clientId: 1,
requestId: 'abc',
hours: 6
},
{
clientId: 2,
requestId: 'ghi',
hours: 1
},
{
clientId: 1,
requestId: 'def',
hours: 4
},
{
clientId: 1,
requestId: 'zzz',
hours: 2
}
]
function allocateAndReport() {
//Initiallizing Result Object
var result = {
butlers: [],
spreadClientIds: []
};
//Setting Max Hours per buttler
const maxHoursPerButler = 8;
//Filtering butlers on the time slots availability
var butlersArr = [];
exampleRequests.map((request, index) => {
//If there is not initial butler avaialble push a new
if (butlersArr.length === 0) {
var remainignHours = maxHoursPerButler - request.hours;
butlersArr.push({
totalHours: maxHoursPerButler,
remainignHours: remainignHours,
requests: [request.requestId]
})
}
// If there is any butler already available
else {
//Check if this butler has enough time to perform this job
var avaialbleButler = butlersArr.filter((but) => but.remainignHours >= request.hours)
// If yes
if (avaialbleButler.length > 0) {
var butler = avaialbleButler[0];
butler.remainignHours = butler.remainignHours - request.hours;
butler.requests.push(request.requestId)
}
// if butler is not having enough capacity, push a new butler
else {
var remainignHours = maxHoursPerButler - request.hours;
butlersArr.push(
{
totalHours: maxHoursPerButler,
remainignHours: remainignHours,
requests: [request.requestId]
})
}
}
})
//Filtering Bulters as per required result
butlersArr.map((butler, index) => {
result.butlers.push(butler.requests);
})
// Filtering unique client Ids
result.spreadClientIds = exampleRequests.map(item => item.clientId)
.filter((value, index, self) => self.indexOf(value) === index)
console.log(result);
}
allocateAndReport();