title: Calculate the LCM of two or three numbers in JavaScript
body: <p>I am using the following code to determine the <strong>Greatest Common Denominator (GCD)</strong> of two or three numbers:</p>
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>Math.GCD = function(numbers) {
    for (var i = 1 ; i &lt; numbers.length ; i++){
      if (numbers[i] || numbers[i] === 0)
        numbers[0] = twogcd(numbers[0], numbers[i]);
    }
    return numbers[0];
  
    function twogcd(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;
      }
     }
  };
  
  Math.LCM = function(first,second) {
      return first * (second / this.GCD(first, second)); // CANNOT FIGURE OUT HOW TO EXTEND THIS TO THREE #s
  };
  
  // example
  console.log(Math.GCD([4, 5, 10]));</code></pre>
  </div>
  </div>
  
  <a href="https://jsfiddle.net/59z28rpk/" rel="nofollow noreferrer">Demo also in JSFiddle</a></p>
  <p>Notice the function in there about the <strong>Least Common Multiple (LCM)</strong></p>
  <p>I am trying to extend this function so that it can compute the LCM of the same two or three user supplied inputs, but I cannot for the life of me get it right. I am a novice at JavaScript and would appreciate any help whatsoever. Note that if a field is left blank it should be omitted from the calculation also, as is done for the GCD.</p>