title: Recursion without overun
body: <p>I've been looking for real world examples of recursion. Remember, programming Wizards, I'm and artist and in Photoshop scripting (scriptus modus operandi) it's normally used to loop over all layers and sub layers.</p>
  
  <p>I'm working on a (simple) recursion script to solve a four digit combination lock. You know, start with 1, then try 2, then 3 etc until the solution is found. To make things easy in the example the second digit is correct so we know that we don't have to change that. Also the initial state the numbers start zero, but we know there are no zeroes in the final solution.</p>
  
  <ul>
  <li>The attempt must match the solution AND also add up to 10 in order to
  be solved.</li>
  </ul>
  
  <p>This may seem a bit stoppid, but I want to put in a two part condition of the solution, mainly because I can then apply what I've learned and write a brute force suduko solver. But you must crawl before you can ice skate...</p>
  
  <pre><code>var puzzle = [0,2,0,0]; // source
  var solution = [1,2,3,4];
  var s = superCopy(puzzle); // working array
  
  
  drawPuzzle(s);
  solvePuzzle(s, puzzle);
  
  var total = checkTotal(s, solution);
  var solution = checkSolution(s, solution);
  
  
  function checkTotal(arr, source)
  {
    var c = 0;
  
    // count the total
    for (var i = 0; i &lt; arr.length; i++)
    {
      c += arr[i];
    }
    if (c == 10)
    {
      alert("Total OK")
      return true;
    }
  }
  
  function checkSolution(arr, source)
  {
    // check the solution
    for (var i in arr)
    {
      if (arr[i] != source[i]) return false
      return true;
    }
  }
  
  function solvePuzzle(arr, source)
  {
    for (var i = 0; i &lt; arr.length; i++)
    {
      // check the source
      var sourceCell = source[i];
      //alert("checking source " + sourceCell)
      //if it's a zero we can change it
      if (arr[i] == 0) 
      {
        cell = arr[i];
  
        cell+=1;
        if (cell &gt; 4) cell = 0;
        arr[i] = cell;
      }
    }
  
    // check the solution
    for (var i in arr)
    {
      // overflow time!
      if (arr[i] != source[i]) solvePuzzle(arr, source)
      else
      {
        alert("All done!")
      }
    }
  }
  
  function drawPuzzle(arr)
  {
    var p = "";
    var c = 0;
    for (var i = 0; i &lt; arr.length; i++)
    {
      if (arr[i] == 0) p += "-" 
      else p += arr[i];
    c+=1;
    }
    alert(p);
  }
  
  function superCopy(arr)
  {
    // returns a true copy of an array
    tempArr = new Array();
    for (var i = 0; i &lt; arr.length; i++)
    {
      if (arr[i] == 0) tempArr[i] = 1 // changed, thanks Nostradamnit!
      else tempArr[i] = arr[i]
    }
    return tempArr
  }
  </code></pre>
  
  <p>The script is incomplete. This what I have so far, it falls over with an overflow error. Note solvePuzzle and checkTotal functions are not called because I realised that solvePuzzle needs to call itself and work out the solution...which is when I ran into overflow problems and got a bit confused.</p>
  
  <p>I realise that this type of question runs dangerously close to a "fix my code" venture, so I'm prepared to put a bounty out for it. Thanks.</p>