**Intermediate Algorithm Scripting: Smallest Common Multiple

How may I change he filters in order to get the LCM of the arrays?

I am trying to get complet these two: smallestCommons([2, 10]) should return 2520.

smallestCommons([1, 13]) should return 360360.

function smallestCommons(arr) {

 let a=[];   
 
for(let i=arr[0];i<=arr[1];i++){
            
    for(let j=arr[0];j<=i;j++){

         if(i%j==0){
            
            a.push(j)
         }                  
         

            }
         

         }    
let evenArr=a.filter(e=>e%2==0)

let oddArr=a.filter(e=>e%2!=0)

let multipliedArr=oddArr.reduce((a,b)=>a*b)
 console.log(oddArr)       
        
let maxEvenNum=Math.max(...evenArr)
   

//console.log(multipliedArr)
//console.log(maxEvenNum)
  
  let b=multipliedArr*maxEvenNum
   //console.log(b)  
   return b;
}


smallestCommons([1,13]);

How do you envision this approach working? Having you describe your approach in words would be helpful.

1 Like

1 I need to get the range of numbers 2 I need to get the multiples of every number in that range 3 I need to know how to employ them to know the smallest number, which is multiple of every number in that range.

1 Get the highest number of the range.
2 Verify if it is divisible by every number in the range.
3 If it is not, add 1.
4 Repeat until you get the number, which is divisible by every number in the array.
I do not understand the reason why this is not working.

function smallestCommons(...arr) {
  
  let counter=arr[0][1];
  let a=[];  
  for(let i=arr[0][0];i<=counter;i++){
          
          if(arr[0][1]%i==0){
                            
                    
          }else{
              
              a.push(i)
          }
   

     }

      console.log(a)   
    while(a.length==0){
       counter=counter+1
        
    }
     console.log(a)
           
}


smallestCommons([1,5]);

Ok, so your code and description don’t match.

for (let i = arr[0][0]; i  <= counter; i++) {
  ...
}

This is saying that you start at arr[0][0] and check values of i until i <= counter === arr[0][1]. If these two arguments are in order, then your start at the smallest, and then execute the loop body.

Exactly. So, I get [2]it is not divisible. Then it should add 1 to counter and, start again until it reached 6 when a= and counter shoud be 6.

The for loop is your outer loop though.

1 Like

It is telling me that there is potential infinite loop.

function smallestCommons(...arr) {
  
  let counter=arr[0][1];
  let a=[];  
  for(let i=arr[0][0];i<=counter;i++){
          
          if(arr[0][1]%i==0){
                            
                    
          }else{
              
              a.push(i)
          }
   
      console.log(a)   
    while(a.length==0){
       counter=counter+1
     }


        
    }
     console.log(a)
           
}


smallestCommons([1,3]);

I want to start the loop again with a new counter, how may I do that?

1To get the highest number.
2 To get the lowest number.
3 To get the range of numbers.
4 To divide the highest number by every number of the range.
5 if there are no multiple number, repeat until you do not get any no multiple number.
6 Return the number where you do not have any multiple number.
I do not understand the reason why this is not working.

function smallestCommons(arr) {
 
let maxNum=Math.max(...arr)
let minNum=Math.min(...arr)
let n=maxNum 
let noMultArr=[];   
   
                   
   for(let i=minNum;i<=maxNum;i++){
       
       while(n%i!=0){

                 console.log(i)  
                 n=n+1
             }
   }       

           
 
  return n;
 
}


smallestCommons([1,5]);

function smallestCommons(arr) {

let maxNum=Math.max(…arr)
let minNum=Math.min(…arr)
let n=maxNum
let noMultArr=;

for(let i=minNum;i<=maxNum;i++){

   while(n%i!=0){

             console.log(i)  
             n=n+1
         }

}

return n;

}

smallestCommons([1,5]);