title: Do I really have to use a global variable for this recursive function?
body: <p>I have a recursive function that happens to be a for loop</p>
  
  <pre><code>function deepFindGroup(groupName, currentGroup) {
      console.log("Testing");
      for (e in currentGroup) {
  
          if (currentGroup[e].intName == groupName) {
              console.log(currentGroup[e]["Members"]);
              return currentGroup[e]["Members"];
          } else if (currentGroup[e]["Members"]) {
              return deepFindGroup(groupName, currentGroup[e]["Members"]);
          }
      }
  }
  </code></pre>
  
  <p>It seems I cannot handle every possibility with a meaningful return statement. If, say, currentGroup[e].intName is not equal to groupName and no child of that branch has a member property (It has no child at all in this case). It ends up returning undefined. The only solution I can think of is making a global variable, I'm trying to avoid that.</p>
  
  <p>edit:
  The function is supposed to return a given branch in a tree. The tree is made up of objects (with an intName properties) that contain "Members" which can themselves be objects containing other Members. Members can also contain nothing but still have an intName.</p>
  
  <p>Looks like that:</p>
  
  <pre><code>Gengroup_1--intName: Gengroup_1
            |
            --Members-- nochild -- intName: nochild
                      |
                      --Gengroup_2--intName: Gengroup2
                                   |
                                   --Members-- object -- intName: object
                                             |
                                             -- anotherObject -- intName: anotherObject
  </code></pre>