List all direct and indirect employees of a manager This is the question:


  You have an Employee class:

class Employee {
    String name;
    Integer id,
    Employee manager
}

  
  Each employee has a manager and the manager of CEO is null. Find all
  direct and indirect employees of a manager.
  
  For example:
  
  
  Say Employee e1 reports to CEO
  e2 and e3 reports to e1
  e4 and e5 reports to e2
  e6 reports to e3
  
  
  Then by giving e1 as input, output should be e2, e3, e4, e5 and e6.


I know I am using the stupidest way to do it. But I wonder if there is any better way to do the if-statement?



function func() {
  function Employee(name, id, manager) {
    this.name = name;
    this.id = id;
    this.manager = manager;
  }

  const ceo = new Employee("ceo", 1, null);
  const e1 = new Employee("e1", 2, ceo);
  const e2 = new Employee("e2", 3, e1);
  const e3 = new Employee("e3", 4, e1);
  const e4 = new Employee("e4", 5, e2);
  const e5 = new Employee("e5", 6, e2);
  const e6 = new Employee("e6", 7, e3);

  var name = document.getElementById("txtName").value;

  var eArr = [];


  if (e1.manager.name === name.toLowerCase()) {
    eArr.push(e1.name);
  }
  if (e2.manager.name === name.toLowerCase() || (e2.manager.manager !== null &amp;&amp; e2.manager.manager.name === name.toLowerCase())) {
    eArr.push(e2.name);
  }
  if (e3.manager.name === name.toLowerCase() || (e3.manager.manager !== null &amp;&amp; e3.manager.manager.name === name.toLowerCase())) {
    eArr.push(e3.name);
  }
  if (e4.manager.name === name.toLowerCase() || (e4.manager.manager !== null &amp;&amp; e4.manager.manager.name === name.toLowerCase()) || (e4.manager.manager.manager !== null &amp;&amp; e4.manager.manager.manager.name === name.toLowerCase())) {
    eArr.push(e4.name);
  }
  if (e5.manager.name === name.toLowerCase() || (e5.manager.manager !== null &amp;&amp; e5.manager.manager.name === name.toLowerCase()) || (e5.manager.manager.manager !== null &amp;&amp; e5.manager.manager.manager.name === name.toLowerCase())) {
    eArr.push(e5.name);
  }
  if (e6.manager.name === name.toLowerCase() || (e6.manager.manager !== null &amp;&amp; e6.manager.manager.name === name.toLowerCase()) || (e6.manager.manager.manager !== null &amp;&amp; e6.manager.manager.manager.name === name.toLowerCase())) {
    eArr.push(e6.name);
  }

  if (eArr.length !== 0)
    document.getElementById("ans").innerHTML = "Ans: " + eArr;
  else
    document.getElementById("ans").innerHTML = "Ans: No reportees";
}
Manager Name = &lt;input type="text" id="txtName"&gt;
&lt;button onclick="func()"&gt;Result&lt;/button&gt;
&lt;p id="ans"&gt;&lt;/p&gt;
Review

You shouldn't put a function definition inside of another function like that. It means it has to recreate the function every time. It would be better to not get in a habit of that.
Same goes for the contants ceo, etc.
In addition, your code only works for one test case, as you manually check for each member.

Rewrite:

const [addEmployee, getEmplyeeBelows] = (function() {
   class Employee {
      constructor(name, id, manager) {
        this.name = name;
        this.id = id;
        this.manager = manager;
        this.manager = registry[manager];
    }
   }
   const registry = {};
   function addEmployee(name, number, parent) {
      if( parent === null ) {
         registry[name] = new Employee(name, number, parent);
      } else {
         parent.emplyees[number] = new Employee(name, number, parent);
         registry[name] = parent.emplyees[number];
      }
   }
   function getEmplyeeBelows(name) {
      let result = [];
      result.push(...Object.values(registry).map((emp) =&gt; {
        return emp.manager == name ? [emp, ...getEmplyeeBelows(emp)] : [];
      })
      return result;
   }
   return [addEmployee, getEmplyeeBelows]
})()
addEmployee('ceo', 1, null);
addEmployee('e1', 2, 'ceo');
addEmployee("e2", 3, 'e1');
addEmployee("e3", 4, 'e1');
addEmployee("e4", 5, 'e2');
addEmployee("e5", 6, 'e2');
addEmployee("e6", 7, 'e3');

const manager = () =&gt; {

  var name = document.getElementById("txtName").value;

  eArr = getEmplyeeBelows(name);

  if (eArr.length !== 0)
    document.getElementById("ans").innerHTML = "Ans: " + eArr;
  else
    document.getElementById("ans").innerHTML = "Ans: No reportees";
}