title: Recursive Return Statement
body: <p>This is a program that I have wrote to solve a problem. Check if there exists a sum of elements that equal to the maximum number in the array, return true if so, false otherwise.</p>
  
  <pre><code>var found = "false";
  var max;
  
  function ArrayAdditionI(array) { 
  
  max = Math.max.apply(null,array);
  var p = array.indexOf(max);
  
  array.splice(p,1);
  array.sort(function(a, b){return a-b;});
  
  
  found = findSum(array, 0, 0);
  
  return found;
  
  }
  
  function findSum(array, sum, startIndex){
  
  for(var i = startIndex; i &lt; array.length ; i++){
  
      sum += array[i]; 
  
      if(sum === max){
          found = "true";
          break;
      }else if(sum &gt; max){
          break;
      }
  
       if(i+2 &lt; array.length &amp;&amp; sum &lt; max){
           findSum(array, sum, i+2);
      }
  
  }
      if(startIndex &lt; array.length &amp;&amp; sum !== max){
          return findSum(array, 0, startIndex+1);
      }
  
      return found;
  }
  
  ArrayAdditionI(readline());
  </code></pre>
  
  <p>I had to use global variable, found, to indicate where a sum has been found or not. The return statement always returned undefined.</p>
  
  <p>Also, if I use a return statement in the following if statement, the code does not work properly.</p>
  
  <pre><code>if(i+2 &lt; array.length &amp;&amp; sum &lt; max){
      return findSum(array, sum, i+2);
  }
  </code></pre>
  
  <p>This is not the optimal solution to the problem, but this is the version I got working. </p>
  
  <p>My question is Why am I getting undefined if I use return statement within the if statement. Also, I tried not using global and use return true if sum === max and at the very end return false, it always returns false or undefined.</p>
  
  <p>-- UPDATE 2: Code with error results --</p>
  
  <pre><code>function ArrayAdditionI(array) { 
      var max = Math.max.apply(null,array);
  
      //remove max element from array
      var p = array.indexOf(max);
      array.splice(p,1);
  
      //sort array
      array.sort(function(a, b){return a-b;});
  
      //call find sum function
      return findSum(array, 0, 0, max);
  
  }
  
  function findSum(array, sum, startIndex){
      for(var i = startIndex; i &lt; array.length ; i++){
  
          sum += array[i];
  
          if(sum === max){
              return true;
          }else if(sum &gt; max){
              break;
          }
  
          if(i+2 &lt; array.length &amp;&amp; sum &lt; max){
              **return** findSum(array, sum, i+2, max);
          }
  
      }
      if(startIndex &lt; array.length &amp;&amp; sum !== max){
          return findSum(array, 0, startIndex+1, max);
      }
  
      return false;
  }
  
  // calling the first function
  ArrayAdditionI([  7, 2,90, 31, 50 ]);           
  </code></pre>
  
  <p>The start of the program is this call: ArrayAdditionI([  7, 2,90, 31, 50 ]); 
  The return should be true. 
  Also, ArrayAdditionI([  1,2,3,4 ]); is true.
  However, ArrayAdditionI([  1,2,3,100 ]); is false.</p>
  
  <p>The return statement between ** **, when removed the code works, otherwise I either get false or undefined. I do not understand this part! Why does removing the return solves the problem, I thought every recursive call must be proceeded with a return statement.</p>
  
  <p>Is the problem maybe due to multiple calls ? Am I using recursion in the improper way?</p>