title: Iterating GCD function in JavaScript
body: <p>I am using this JavaScript function to determine the GCD of two values obtained from input fields:</p>
  
  <pre><code>Math.GCD = function(first,second) {
      if (first &lt; 0) first = -first;
      if (second &lt; 0) second = -second;
      if (second &gt; first) {var temp = first; first = second; second = temp;}
      while (true) {
          first %= second;
          if (first == 0) return second;
          second %= first;
          if (second == 0) return first;
      }
  };
  </code></pre>
  
  <p>I would like to extend this to compute the GCD of three numbers, if the user enters a number into a third input field (otherwise, the user will input two and calculate as per this function). Being relatively new to JavaScript, I am not sure how to extend this function for three values. Can anyone help?</p>
  
  <p>Fiddle: <a href="https://jsfiddle.net/tjj7won4/1/" rel="nofollow">https://jsfiddle.net/tjj7won4/1/</a> </p>
  
  <p>Also, I would like to determine the LCM in a likewise fashion, as observed in the fiddle, but, again, I am unsure how to extend the given function. Please, help.</p>