Smallest Common Multiple - Help

Here is my solution to the problem but its hanging my browser.

function smallestCommons(arr) {

 // 1 , 5
 i=2;
match = false;
count = 0;
j = arr[0]+1;
inner = false;

newArr = [];

for(i=arr[0]+1; i<arr[1];i++)
{
  newArr.push(i);
}

while(match === false)
{
  if(arr[0] < arr[1])
  {
    if(i%arr[0] ===0 && i%arr[1] === 0){
      
      while(inner === false){
        if(i%j===0) // 2 % 2 = true; 3%2 false; 4%2 = true;
          {
            count++;
          }
        else{
          inner = true;
          count = 0;
          j = arr[0]+1;
          break;
        }
        
        j++;
        
        if(newArr.length === count)
          {
            match = true;
            break;
          }
      }
    }else{
      i++;
    }
  }
else{

}
} 
return arr;
}
smallestCommons([1,5]);

if(i%arr[0] ===0 && i%arr[1] === 0){

bcoz of this condition i always be 5 and bcoz i is 5

if(i%j===0) // 2 % 2 = true; 3%2 false; 4%2 = true;

this condition wont run

I re wrote the solution. Here is another way of solving this challenge.

function smallestCommons(arr) {
  
  // 1 , 5
  i=2;
  match = false;
  count = 0;
  j = arr[0]+1;
  inner = false;
  less = false;
  num = 0;
  newArr = [];
  
  
  if(arr[0] < arr[1])
	{
		j = arr[0]+1;
		for(i=arr[0]+1; i<arr[1];i++)
		{
			newArr.push(i);
		}
	}
	else{
		j = arr[1]+1;
		for(i=arr[1]+1; i<arr[0];i++)
		{
			newArr.push(i);
		}
	}

  console.log(arr[1]);
  
  while(match === false)
	{  //noprotect
	  if(arr[0] < arr[1])
	  {
		if(i%arr[0] === 0 && i%arr[1] === 0){
			while(inner === false){
				if(i%j===0) // 2 % 2 = true; 3%2 false; 4%2 = true;
				  {
					//console.log("Match from inner " + i);
					count++;
				  }
				else{
				  inner = true;
				  count = 0;
				  j = arr[0]+1;
				  break;
				}

				j++;
				
				if(newArr.length === count)
				  {
					num = i;
					match = true;
					break;
				  }
			  }
			  
			  inner = false; 
		}
	  }
	  else{
//        console.log(arr[0] +" match not found " + arr[1] + " length " + newArr.length);
		
		if(i%arr[1] === 0 && i%arr[0] === 0){
			while(inner === false){
				if(i%j===0) // 2 % 2 = true; 3%2 false; 4%2 = true;
				  {
					count++;
					console.log("Match from inner " + i + " count value " + count);
				  }
				else{
				  inner = true;
				  count = 0;
				  j = arr[1]+1;
				  break;
				}

				j++;
				
				if(newArr.length === count)
				  {
					console.log("Perfect Match " + i);
					num = i;
					match = true;
					break;
				  }
			  }
			  
			  inner = false; 
		}
	  }
	 
	  i++;
	  
	  if( num !==0)
		{
                      console.log(i);
		  match = true;
		  break;
		}
	}
  
  return num;
}

smallestCommons([23,18]);

google around the subject, and you’ll find that the smallest common multiple of A & B is equal to A times B divided by the greatest common divisor of A&B.

eg, A=4, b=10, the greatest common divisor of A&B = 2, so 4*10/2=20.

for finding the least common multiple of a series A, B, C, D, you can start with the first two, then repeat the process with the results against C, then the results of that against D.

Take a stab at figuring out how to calculate a greatest common divisor of two numbers, and if you’re having trouble, check http://stackoverflow.com/questions/17445231/js-how-to-find-the-greatest-common-divisor for two different approaches.

1 Like

i think i have the simple solution.

function smallestCommons(arr) {
    var maxValue = Math.max(...arr);
    var minValue = Math.min(...arr);
    var arr2 = [];
    for (var i = minValue; i <= maxValue; i++) arr2.push(i);
    var higestValueTable = 0;
    function commonMultiple(value) {
        return (higestValueTable % value === 0);
    }

    var countTable = 1;
    var commonValue = 0;
    while (true) {
        higestValueTable = maxValue * countTable;
        countTable++;
        if (arr2.every(commonMultiple)) {
            commonValue = higestValueTable;
            break;
        }
    }
    return higestValueTable;
}


smallestCommons([23,18]);