/**
 * @name findPath
 * @description Function that is called at the beginning of each point, expecting
 * an array of directions to be given to reach the X,Y coordinates of the square
 * with the point on. The game will pause if the snake crashes or if the point
 * is never reached.
 *
 * @param {Number} xMax Number of cells across the x axis.
 * @param {Number} yMax Number of cells across the y axis.
 * @param {Array} snake Array of [X,Y] cords of the snake from head to tail.
 * @param {Array} point [X, Y] coordinates of the point.
 * @param {Object} D Constant directions to be returned (UP, RIGHT, DOWN, LEFT)
 *
 * @return {Array} Moves for each cell i.e [D.UP, D.UP, D.RIGHT]
 */
function findPath(xMax, yMax, snake, point, DIR) {
  const U = DIR.UP, D = DIR.DOWN, L = DIR.LEFT, R = DIR.RIGHT;
  const moves = [];
  const [pX, pY] = point;
  
  function shift(x, y, d) {
    switch (d) {
      case U: return [x, y - 1];
      case R: return [x + 1, y];
      case D: return [x, y + 1];
      case L: return [x - 1, y];
    }
  }
  
  function heuristic([x, y]) {
    return Math.abs(x - pX) + Math.abs(y - pY);
  }
  
  function isValid([, [x, y]]) {
    return x >= 0 && x < xMax && y >= 0 && y < yMax &&
      !snake.some(([sX, sY]) => x === sX && y === sY);
  }
  
  function byHeuristic([,, aH], [,, bH]) {
    return aH - bH;
  }
  
  let [[x, y]] = snake;
  
  while (x !== pX || y !== pY) {
    const SU = shift(x, y, U);
    const SR = shift(x, y, R);
    const SD = shift(x, y, D);
    const SL = shift(x, y, L);
    const winner = [
      [U, SU, heuristic(SU)],
      [R, SR, heuristic(SR)],
      [D, SD, heuristic(SD)],
      [L, SL, heuristic(SL)],
    ].filter(isValid).sort(byHeuristic).shift();
    
    if (!winner) {
      return moves; 
    }
    
    const [d, p] = winner;
    
    moves.push(d);
    [[x, y]] = [p].concat(snake).slice(0, -1);
  }
  
  return moves;
}

@@ -748,107 +748,97 @@
 nst 
-U = DIR.UP, D = DIR.DOWN, L = DIR.LEFT, R = DIR.RIGHT;%0A  const moves = %5B%5D;%0A  const %5BpX, pY%5D = point
+moves = %5B%5D;%0A  const %5BpX, pY%5D = point;%0A  const %7B UP: U, DOWN: D, LEFT: L, RIGHT: R %7D = DIR
 ;%0A  
@@ -1360,16 +1360,31 @@
 nake;%0A  
+let index = 0;%0A
 %0A  while
@@ -1717,16 +1717,8 @@
 tic)
-.shift()
 ;%0A  
@@ -1735,16 +1735,19 @@
 (!winner
+%5B0%5D
 ) %7B%0A    
@@ -1799,16 +1799,19 @@
 = winner
+%5B0%5D
 ;%0A    %0A 
@@ -1832,24 +1832,21 @@
 d);%0A    
-%5B%5Bx, y%5D%5D
+snake
  = %5Bp%5D.c
@@ -1872,16 +1872,38 @@
 0, -1);%0A
+    %5B%5Bx, y%5D%5D = snake;%0A
   %7D%0A  %0A 

Update 1-A*-search-each-move.js