This problem set explores a number of questions related to a company and its employees; specifically, its hierarchy of management:
const employees = [
{ name: 'Alice', manager: null, title: 'CEO' },
{ name: 'Bob', manager: 'Alice', title: 'Head of Marketing' },
{ name: 'Carol', manager: 'Alice', title: 'Head of Engineering' },
{ name: 'Darcy', manager: 'Carol', title: 'Systems Architect' },
{ name: 'Edwin', manager: 'Bob', title: 'SEO Specialist' },
{ name: 'Farah', manager: 'Carol', title: 'Senior Developer' },
{ name: 'Georg', manager: 'Carol', title: 'Junior Developer' }
];
-
Write a function that takes the employee list and outputs the number of direct reports each employee has. For this dataset, the output would be:
{ 'Alice': 2, 'Bob': 1, 'Carol': 3, 'Darcy': 0, 'Edwin': 0, 'Farah': 0, 'Georg': 0 }
-
Write a function that takes the employee list and outputs another array that contains an array of the names of an employee's direct reports instead of their manager's name, e.g.:
[ { name: 'Alice', reports: ['Bob', 'Carol'], title: 'CEO' }, ... ]
-
Modify the function from #2 to include indirect reports, too; i.e. if Alice is Bob's manager and Bob is Edwin's manager, Edwin is one of Alice's indirect reports.
-
Write a function that takes the employee list and returns the depth of the employee hierarchy at the company. For this company, the depth is
3
. A completely flat hierarchy (no managers) has a depth of1
. -
Suppose we were to change the dataset slightly and change Alice's manager to Darcy:
{ name: 'Alice', manager: 'Darcy', title: 'CEO (???)' }
What would happen if you ran the code you wrote in #3 and #4? What is this type of error called? Write a function to detect if the dataset contains any of these problems, returning
true
if the dataset is invalid (contains any of these problems) andfalse
otherwise.