The function would return a different result depending on which 1
you turn to 0
so "as far as possible" is relative to that.
Just make it so that if the path is empty you fill it this way:
currentX = startX;
currentY = startY;
path = [];
while(grid[currentY][currentX] != 1){
path.push({x: currentX, y: currentY});
dX = endX - currentX;
dY = endY - currentY;
distanceX = Math.abs(dX);
distanceY = Math.abs(dY);
directionX = dX / distanceX;
directionY = dY / distanceY;
// Make a step in the direction where the distance is bigger
if(distanceX > distanceY){
currentX += directionX;
}else{
currentY += directionY;
}
}
It'll be a sort of straight line to the destination interrupted by the wall.