-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputOutput Challenge
More file actions
148 lines (110 loc) · 4.55 KB
/
Copy pathInputOutput Challenge
File metadata and controls
148 lines (110 loc) · 4.55 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
You are given an input file containing an array of work hours for a company such as [9,9,9,9,8,0,0] where the first element represents the number
of work hours on Monday and the seventh element represents the number of work hours on Sunday, a list of employees where there is an id, name,
and optionally an array of custom work hours per employee. Implement a method that a takes an employee id, start date, and end date as inputs
and outputs a list of available hours by date for that employee.
Employee's custom work hours are defined by one or more work hour blocks such as:
1, "01/01/2016", "12/31/2016", [4,4,8,8,4,0,0]
1, "01/01/2017", null, [8,8,8,8,0,0,0]
where each block has a user id, an optional start or end dates, and an array of hours in a standard work week during that time period.
The output should be sorted by date, and be formatted as below:
"12/06/2016", 0
"12/07/2016", 4
"12/08/2016", 4
"12/09/2016", 8
Example input file
Ignore blank lines and any lines that starts with a #
# Company work hours
[9,9,9,9,8,0,0]
# Employees
1, Michele
2, Bill
3, Chris
4, Alex
# Michele’s availability
1,"01/01/2016","12/16/2016", [4,10,10,10,4,0,0]
1,"01/01/2017",null,[8,8,8,8,8,0,0]
# Bill’s availability
2,"02/13/2017","02/27/2017", [4,4,4,4,4,0,0]
# Alex’s availability
4,"04/10/2017","04/16/2016",[9,0,0,0,0,0,0]
// code
"use strict";
const data = require("./input.json");
const application = {
availableHoursByEmployee: (employeeID, startingDate, endingDate) => {
const thisEmployee = application.checkEmployee(employeeID);
if (thisEmployee === undefined || !application.checkDate(startingDate, endingDate)) {
console.log("Invalid Parameters \n ----------\nMake sure you have the correct employee ID and valid START and END dates \n");
}
else {
application.betweenDates(startingDate, endingDate).map((date) => {
const dayForHours = (application.dayOfWeekMap(date.getDay()));
console.log('"' + application.formatDate(date) + '", ' + Math.min(application.checkEmployeeCustomHours(employeeID, date), data.companyWorkHours[dayForHours]));
});
}
},
betweenDates: (startDate, endDate) => {
const requestedDateRange = [],
currentDate = new Date(startDate),
stopDate = new Date(endDate);
while (currentDate <= stopDate) {
requestedDateRange.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
return requestedDateRange;
},
checkEmployee: (empID) => {
return data.employees[empID - 1] ? (data.employees[empID - 1]) : undefined;
},
checkEmployeeCustomHours: (empID, date) => {
const thisEmployee = application.checkEmployee(empID),
len = thisEmployee.customHours.length,
availableHours = application.dayOfWeekMap(date.getDay());
for (let i = 0; i < len; i++) {
if (date >= new Date(thisEmployee.customHours[i].startDate) && date <= new Date(thisEmployee.customHours[i].endDate)) {
return thisEmployee.customHours[i].hours[availableHours];
}
else if (date > new Date(thisEmployee.customHours[i].endDate)) {
i += 1;
}
else if (date >= new Date(thisEmployee.customHours[i].startDate && new Date(thisEmployee.customHours[i].endDate === null))) {
return thisEmployee.customHours[i].hours[availableHours];
}
}
//returning 25 because it is outside the range of possible work hours and Math.min will grab the company's work hours in availableHours() instead.
return 25;
},
checkDate: (startDate, endDate) => { return (new Date(endDate) >= new Date(startDate)) },
//Work week begins with Monday not Sunday in this scenario, this is to correct getDay() values
dayOfWeekMap: (day) => {
const daysOfWeek = {
1: 0,
2: 1,
3: 2,
4: 3,
5: 4,
6: 5,
0: 6
};
return daysOfWeek[day];
},
displayAllEmployeeIDs: () => {
data.employees.map((employee) => {
console.log(employee.id + " : " + employee.name);
});
},
formatDate: (value) => { return value.toLocaleDateString(); }
};
module.exports = application;
//successfull cases
// application.availableHoursByEmployee(1, "12/15/2016", "01/20/2017");
// application.availableHoursByEmployee(2, "01/15/2017", "02/20/2017");
// application.availableHoursByEmployee(5, "01/01/2015", "01/20/2015");
// application.availableHoursByEmployee(3, "01/01/2015", "01/01/2015");
//invalid cases
// application.availableHoursByEmployee(8, "01/15/2016", "01/20/2016");
// application.availableHoursByEmployee(2, "01/50/2016", "01/20/2016");
// application.availableHoursByEmployee(1);
// application.availableHoursByEmployee("01/23/2015");
// see all employees and their IDs
// application.displayAllEmployeeIDs();