Javascript pathfinder function based on A* algorithm