title: Is it possible to replace two opposite and different functions with a single one?
body: <p>I want to optimise and reduce my code to increase performance and correct-ability of it. With those two different functions below I can successfuly move a <code>Google Map Marker</code> on a map forward and backward using a <code>pathIndex</code>, calcuted on an array of GPS coordinates [I didn't include this section of code since I think it's not releated to this question but I can and will post it if needed].</p>
  
  <p>This is my code:</p>
  
  <p><strong>1st function</strong>:</p>
  
  <pre><code>function animate() {
      if (pathIndex &lt; coord.length &amp;&amp; mapAnimationStatus != PLAY_PAUSED) {
          googleMapsMarker.setPosition(coord[pathIndex]);
          googleMap.panTo(coord[pathIndex]);
  
          pathIndex += 1;
  
          if (pathIndex == coord.length) {
              pause();
  
              pathIndex = 0;
              mapAnimationStatus = NOT_PLAY;
  
              return;
          }
  
          timerHandler = setTimeout("animate(" + pathIndex + ")", 1000);
      }
  }
  </code></pre>
  
  <p><strong>2nd function</strong>:</p>
  
  <pre><code>function animateRewind() {
      if (pathIndex &gt;= 0 &amp;&amp; mapAnimationStatus != PLAY_PAUSED) {
          googleMap.panTo(coord[pathIndex]);
          googleMapsMarker.setPosition(coord[pathIndex]);
  
          if (pathIndex == 0) {
              pause();
  
              mapAnimationStatus = NOT_PLAY;
  
              return;
          }
  
          pathIndex -= 1;
  
          timerHandler = setTimeout("animateRewind(" + pathIndex + ")", 1000);
      }
  }
  </code></pre>
  
  <p>As you can see those two functions shares a lot of portions of code and it think that they can be replaced with a single one for this reason but I can't figure out how to do this.</p>
  
  <p>So, is it possible to create a single <code>function</code> to manage those two different animations?</p>