Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Given an array of Person objects, returns the root PersonTreeNode (the CEO).
- * @param {Person[]} employees - An array of Person objects representing all the employees of the company.
- * @returns {PersonTreeNode} The CEO of the organization.
- */
- function generateTree( employees ) {
- /**
- * @ignore
- * INSTRUCTIONS:
- * 1. ONLY edit this function and nothing else!.
- *
- * 2. Analyze the Person.js and PersonTreeNode.js files.
- *
- * 3. Parse the `employees` array and create a single PersonTreeNode
- * object representing the CEO (the Person with no `manager`).
- * All PersonTreeNode object's `directReports` arrays should contain
- * PersonTreeNode's for their direct reports...creating a tree.
- *
- * 4. Refresh or click the 'Retry Test' button to rerun the test.
- *
- * Feel free to create any additional functions in this file as needed.
- */
- var ceo = null; // Should be a PersonTreeNode object at the end;
- // YOUR CODE STARTS HERE
- let employeeList = {};
- // used when adding employee to list and converting name to an id
- let mgrNode = {};
- // chain all the things like an adult
- ceo = employees.map((employee) => new PersonTreeNode(employee))
- // add employe node to employee list
- .map((employee) => {
- employeeList[employee.person.id] = employee;
- mgrNode[employee.person.name] = employee;
- return employee;
- })
- // add curret employee to directReports
- .map((employee) => {
- if (employee.person.manager) {
- let mgrId = employee.person.manager.id;
- employeeList[mgrId].directReports.push(employee);
- }
- return employee;
- })
- // find the ceo
- .reduce( ( ceoNode, employee ) => (
- // rule: cep has no manager(s)
- employee.person.manager === null ? employee : ceoNode ), null );
- // YOUR CODE ENDS HERE
- return ceo;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement