title: Is my algorithm wrong or it's right and just needs tweaks?
body: <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;html&gt;&lt;head&gt;&lt;/head&gt;
  &lt;body&gt;&lt;script&gt;
    function GCD(a, b) {
      if (a == 0) {
        return b;
      }
      return GCD(b % a, a);
    }
  
    function difference(array) {
      for (var i = Math.min(...array) + 1; i &lt; Math.max(...array); i++) {
        array.push(i);
      }
      array.sort((a, b) =&gt; a - b);
    }
  
    function smallestCommons(arr) {
      difference(arr);
      console.log(arr);
      a = arr[arr.length - 1];
      b = arr[arr.length - 2];
      var LCM = a * b / GCD(a, b);
      while (true) {
        var index = arr.findIndex(element =&gt; LCM % element !== 0);
        if (index === -1) {
          return LCM;
        }
        LCM *= arr[index];
        console.log(LCM);
      }
    }
    smallestCommons([1, 5]) // right 
    smallestCommons([2, 10]) // right
    smallestCommons([1, 13]) // wrong
    smallestCommons([23, 18]) // wrong
  &lt;/script&gt;&lt;/body&gt;
  &lt;/html&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>This is a code for this challlenge:
  <a href="https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/" rel="nofollow noreferrer">https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple/</a></p>
  
  <p>The algorithm I use:</p>
  
  <p>1-Calculate LCM of the two greatest numbers.</p>
  
  <p>2- Try LCM % number on all the array and if you find a number that doesn’t return 0, assign it to the var index and multiply it * LCM then keep doing like that until it doesn’t find anything and returns -1 instead, when that happens, return LCM finally.</p>
  
  <p>The code works at the first two arrays but it doesn’t work at last two which caused me to think, is my algorithm wrong and I’m just wasting my time trying to tweak it or it’s right and it just needs some tweaks?</p>
  
  <p>Notice that there are 3 different functions, the first function calculates GCD to calculate LCM later, then the second function pushes the values between the two values in the array and then sorts it, and the third function calculates LCM(Lowest Common Multiple)</p>
  
  <p>The problem is that the last second arrays:</p>
  
  <p>smallestCommons([1, 13]) returns 4324320 instead of 360360</p>
  
  <p>smallestCommons([23, 18]) returns 72681840 instead of 6056820</p>
  
  <p>So what I want to know, please focus on this:</p>
  
  <p>Is my whole way(algorithm) wrong and I need to rewrite a whole one or it needs some tweaks to work? </p>
  
  <p>Please, don't give me ready codes. Just tell me what I want to know and thank you (:</p>