title: A* Pathfinding, path not showing up
body: <p>I have this code, in script (p5.js), I am running it and works as expected, but the moment I include the path code, that helps in finding the previous parents to form a path to the end goal, the browser crushes, as the images show. It fist colors the 3 cells then crashes. Here is the code.</p>
  <pre><code>    var col = 12, row = 12, grid = new Array(col), openSet = [], closeSet = [], start, end, w, h, path = [];
  
  function removefromArray(array_, element){
      for(var i = array_.length - 1; i &gt;= 0; i--){
          if(array_[i] == element){
              array_.splice(i, 1);
          }
      }
  }
  
  function heuristic(a, b){
      var distance = abs(a.i - b.i) + abs(a.j - b.j);
      return distance;
  }
  
  function Spot(i, j){
      this.i = i;
      this.j = j;
      this.f = 0;
      this.g = 0;
      this.h = 0;
      this.neighbor = [];
      this.parent = undefined;
      this.wall = false;
      
  
      this.show = function (color){
          
          fill(color);
          if(this.wall){
              fill(0);
          }
          noStroke();
          rect(this.i * w, this.j * h, w - 1, h - 1);
      }
  
      this.addNeighbor = function(grid){
          var i = this.i, j = this.j;
          if(i &lt; col - 1){
              this.neighbor.push(grid[i+1] [j]);
          }
          if(i &gt; 0){
              this.neighbor.push(grid[i-1] [j]);
          }
          if(j &lt; row-1){
              this.neighbor.push(grid[i] [j+1]);
          }
          if(j &gt; 0){
              this.neighbor.push(grid[i] [j-1]);
          }
      }
  
  }
  
  function setup(){
      createCanvas(500,500);
      console.log(&quot;A*&quot;);
  
      w = width / col;
      h = height / row;
      for( var i = 0; i&lt; col; i++){
          grid[i] = new Array(row);
      }
      //Adding a spot
      for( var i = 0; i&lt; col; i++){
          for( var j = 0; j&lt; row; j++){
              grid[i][j] = new Spot(i,j); 
          }
      }
      //Adding a neighbor
      for( var i = 0; i&lt; col; i++){
          for( var j = 0; j&lt; row; j++){
              grid[i][j].addNeighbor(grid);  
          }
      }
      start = grid[0][0];
      end = grid[col - 1][row - 1];
  
      openSet.push(start);
  }
  function draw(){
      var winner = 0;
      if(openSet.length &gt; 0){
          for( var i = 0; i&lt; openSet.length; i++){
              if(openSet[i].f &lt; openSet[winner].f){
                  winner = i; 
              }
          }
          var current = openSet[winner]; 
          if(current === end){
              noLoop();
              console.log(&quot;Done!&quot;);
          }
  
          removefromArray(openSet, current);
          closeSet.push(current);
  
          var neighbors = current.neighbor;
          for(var i = 0; i &lt; neighbors.length; i++){
              var the_neighbor = neighbors[i];
  
              if(!closeSet.includes(the_neighbor)){
                  var tempG  = current.g + 1;
              }
              if(openSet.includes(the_neighbor)){
                  if(tempG &lt; the_neighbor.g){
                      the_neighbor.g = tempG;
                  }
              }
              else{
                  the_neighbor.g = tempG;
                  openSet.push(the_neighbor);
              }
              the_neighbor.h = heuristic(the_neighbor, end);
              the_neighbor.f = the_neighbor.g + the_neighbor.h;
              the_neighbor.parent = current; // the previous node
          }
      }
      else{
          // no solution
      }
  
      background(0)
      for( var i = 0; i&lt; col; i++){
          for( var j = 0; j&lt; row; j++){
              grid[i][j].show(color(255)); 
          }
      }
  
      for( var i = 0; i&lt; openSet.length; i++){
          openSet[i].show(color(&quot;green&quot;));
      }
  
      for( var i = 0; i&lt; closeSet.length; i++){
          closeSet[i].show(color(&quot;red&quot;));
      }
  
      // path = [];
      // var temp = current;
      // path.push(temp);
      // while(temp.parent){
      //     path.push(temp.parent);
      //     temp = temp.parent;
      // }
  
      // for(var i = 0; i &lt; path.length; i++){
      //     path[i].show(color(0,0,255));
      // }
      
  }
  </code></pre>
  <p>If I try to remove the comments slash for this last part, the system will run for about 5secs then crashes. Someone with the solution, I will highly appreciate.</p>
  <p>Result when the path part is uncommented
  <a href="https://i.stack.imgur.com/z1rXg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/z1rXg.png" alt="enter image description here" /></a></p>
  <p><a href="https://i.stack.imgur.com/rbNCJ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rbNCJ.png" alt="enter image description here" /></a></p>