title: Backtracking recursion to find a path from source to destination in 2D array
body: <p>I am trying to find a possible path inside 2D array and I have a source point (x,y) and a destination point (destX, destY).</p>
  
  <p>I am trying to do it in a backtracking recursion way since I don't care about finding the <em>shortest</em> path, all I care about is finding a path.</p>
  
  <p>The problem is that somehow within the algorithm it goes right to a corner and gets stuck there... So I guess I wrote something incorrectly logic wise.</p>
  
  <p>The recursion function:</p>
  
  <pre><code>$scope.findPath = function(x, y, destX, destY) {
          console.log("x: " + x + ", y: " + y);
          if(x &gt;= $scope.buttons.length || x &lt; 0 || y &gt;= $scope.buttons[0].length || y &lt; 0) {
              return false;
          }
  
          if(x == destX &amp;&amp; y == destY) {
              return true;
          }
  
          if(!$scope.checkIfButtonEmpty(x, y)) {
              console.log("location not empty")
              return false;
          }
  
          $scope.solution[x][y].marked = true;
  
          if($scope.findPath(x + 1, y, destX, destY) === true) {
              return true;
          }
          if($scope.findPath(x, y + 1, destX, destY) === true) {
              return true;
          }
          if($scope.findPath(x - 1, y, destX, destY) === true) {
              return true;
          }
          if($scope.findPath(x, y - 1 , destX, destY) === true) {
              return true;
          }
  
          $scope.solution[x][y].marked = false;
  
          return false;
      };
  </code></pre>
  
  <p>The function that calls the recursion, and after finding a path and writing it into a boolean 2D array is supposed to print the path graphically:</p>
  
  <pre><code>$scope.startDrawingConnection = function() {
          if($scope.startLocation.length == 2 &amp;&amp; $scope.endLocation.length == 2){
              $scope.findPath($scope.startLocation[0], $scope.startLocation[1], $scope.endLocation[0], $scope.endLocation[1]);
              console.log("Finished finding path");
              $scope.drawPath();
              console.log("Finished drawing path");
          }
      };
  </code></pre>
  
  <p>Please help me figure out what I did wrong in the algorithm.</p>