title: Recursion, example of application of the recursion
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>let company = { 
    sales: [{name: 'John', salary: 1000}, {name: 'Alice', salary: 1600 }],
    development: {
      sites: [{name: 'Peter', salary: 2000}, {name: 'Alex', salary: 1800 }],
      internals: [{name: 'Jack', salary: 1300}]
    }
  };
  
  // The function to do the job
  function sumSalaries(department) {
    if (Array.isArray(department)) { // case (1)
      return department.reduce((prev, current) =&gt; prev + current.salary, 0); // sum the array
    } else { // case (2)
      let sum = 0;
      for (let subdep of Object.values(department)) {
        sum += sumSalaries(subdep); // recursively call for subdepartments, sum the results
      }
      return sum;
    }
  }
  
  alert(sumSalaries(company)); // 7700</code></pre>
  </div>
  </div>
  
  This code is about using recursion in calculation of total salary in company's departments.
  Please, explain to me this. In the example of tutorial, recursive traversals, after running the first case (we calculate the salary in sales department) function return this salary. So why it proceed further and calculate another case (development department) if we have return statement in first calculation? Shouldn't it break the flow? And how it sum the case 1 and total sum of case 2?</p>