Smallest Common Multiple Is my whole algorithm is wrong?

Smallest Common Multiple Is my whole algorithm is wrong?
0.0 0


Tell us what’s happening:
So yeah, I rewrote my last algorithm because it was heavy on freecodecamp. The algorithm I use this time is:
1-Calculate LCM of the two greatest numbers.
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.
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?
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)

Your code so far

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 < Math.max(...array);i++) {
  array.sort((a,b) => a - b);
function smallestCommons(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 => LCM % element !== 0);
  if(index === -1) {
  return BCD;
  LCM *= arr[index];

smallestCommons([2, 10])

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:


I solved it. arr[index] should be divided by GCD to be a prime number(indivisble) so that it returns the right and lowest result.