-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAmceEmployees(Recursion).js
More file actions
174 lines (146 loc) · 4.4 KB
/
Copy pathAmceEmployees(Recursion).js
File metadata and controls
174 lines (146 loc) · 4.4 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
const employees = [
{ id: 1, name: 'moe'},
{ id: 2, name: 'larry', managerId: 1},
{ id: 4, name: 'shep', managerId: 2},
{ id: 3, name: 'curly', managerId: 1},
{ id: 5, name: 'groucho', managerId: 3},
{ id: 6, name: 'harpo', managerId: 5},
{ id: 8, name: 'shep Jr.', managerId: 4},
{ id: 99, name: 'lucy', managerId: 1}
];
const spacer = (text)=> {
if(!text){
return console.log('');
}
const stars = new Array(5).fill('*').join('');
console.log(`${stars} ${text} ${stars}`);
}
spacer('findEmployeeByName Moe')
// given a name and array of employees, return employee
const findEmployeeByName=(name,employees)=>{
for(let employee of employees){
if(employee.name === name){
return employee
}
}
}
console.log(findEmployeeByName('moe', employees));//{ id: 1, name: 'moe' }
spacer('')
spacer('findManagerFor Shep')
//given an employee and a list of employees, return the employee who is the manager
const findManagerFor=(employee,employees)=>{
let employee2 = findEmployeeByName('shep Jr.', employees)
for(let employee of employees){
if(employee.id === employee2.managerId){
return employee
}
}
}
console.log(findManagerFor(findEmployeeByName('shep Jr.', employees), employees));//{ id: 4, name: 'shep', managerId: 2 }
spacer('')
spacer('findCoworkersFor Larry')
//given an employee and a list of employees, return the employees who report to the same manager
const findCoworkersFor=(employee,employees)=>{
let output = []
let manager = findEmployeeByName('larry', employees)
for(let employee of employees){
if(employee.managerId === manager.managerId && employee.name !== manager.name){
output.push(employee)
}
}
return output
}
console.log(findCoworkersFor(findEmployeeByName('larry', employees), employees));/*
[ { id: 3, name: 'curly', managerId: 1 },
{ id: 99, name: 'lucy', managerId: 1 } ]
*/
spacer('');
spacer('findManagementChain for moe')
//given an employee and a list of employees, return a the management chain for that employee.
//The management chain starts from the employee with no manager with the passed in employees manager
const findManagementChain=(employee,employees)=>{
let output = []
let baseEmplyoee = findEmployeeByName(employee, employees);
output = employees.filter(employees=>employees.id>=baseEmplyoee.managerId)
//sort the output list of qualifying Managers
return output
}
console.log(findManagementChainForEmployee(findEmployeeByName('moe', employees), employees));//[ ]
spacer('');
spacer('findManagementChain for shep Jr.')
console.log(findManagementChainForEmployee(findEmployeeByName('shep Jr.', employees), employees));/*
[ { id: 1, name: 'moe' },
{ id: 2, name: 'larry', managerId: 1 },
{ id: 4, name: 'shep', managerId: 2 }]
*/
spacer('');
spacer('generateManagementTree')
//given a list of employees, generate a tree like structure for the employees, starting with the employee who has no manager. Each employee will have a reports property which is an array of the employees who report directly to them.
console.log(JSON.stringify(generateManagementTree(employees), null, 2));
/*
{
"id": 1,
"name": "moe",
"reports": [
{
"id": 2,
"name": "larry",
"managerId": 1,
"reports": [
{
"id": 4,
"name": "shep",
"managerId": 2,
"reports": [
{
"id": 8,
"name": "shep Jr.",
"managerId": 4,
"reports": []
}
]
}
]
},
{
"id": 3,
"name": "curly",
"managerId": 1,
"reports": [
{
"id": 5,
"name": "groucho",
"managerId": 3,
"reports": [
{
"id": 6,
"name": "harpo",
"managerId": 5,
"reports": []
}
]
}
]
},
{
"id": 99,
"name": "lucy",
"managerId": 1,
"reports": []
}
]
}
*/
spacer('');
spacer('displayManagementTree')
//given a tree of employees, generate a display which displays the hierarchy
displayManagementTree(generateManagementTree(employees));/*
moe
-larry
--shep
---shep Jr.
-curly
--groucho
---harpo
-lucy
*/