Finding smallest multiple of given array

Tell us what’s happening:
I try most of the things but perhaps my code is not fit into my concept of finding multiple elements of the given array.

Your code so far


function giver( n,arr){
 let  a= 0 ;
 let newArr=[] ;
 var result  ;
 var i =0 ;
 var num =5;
   for (let i in newArr){
   if (newArr[i] % arr[a] !== 0){  
   a = a+1 ;
   i=0 ;
 }else { result  = true};  
 }
  for (let i=1; i<=n; i++)
  newArr.push(num*i)  ;
console.log(newArr)
return result ;
}

console.log(giver(12,[1,2,3,4,5]))

function smallestCommons(arr) {
 var num = 1 ;
 var a = [];
 for (let i =arr[0]; i <= arr[1];i++)
    a.push(i); 
 for (let i=0; i<a.length;i++){
  
 }
 return a;
}


// console.log(smallestCommons([1,5]));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:

I’m unable to understand your code for the giver function

your num is fixed

as newArr is empty, newArr[i] will always be undefined…

As far as the Challenge is considered, you’ve to find Least common multiple of all the elements present in the given range…
For Example…
smallestCommons([1,4]) should give 12, as LCM of (1,2,3,4) is 12…

Here’s a little tip:

create a new function to take 2 number as arguments and return their LCM…
For example…

let lcm = 1;
for(let i of arr){
    lcm = LCM(lcm, arr); //return lcm
}

if arr = [1,2,3, 4]
then above function will go like these

  • lcm = 1
  • lcm = LCM(1,1), which will be 1
  • lcm = LCM(1,2), lcm of 1 & 2 is 2
  • lcm = LCM(2,3), lcm of 2 & 3 is 6
  • lcm = LCM(6,4), lcm of 6 & 4 is 12
    Therefore it’ll end up with lcm=12 which is Smallest common multiple of [1, 2, 3, 4]

I hope this is clear to you, I’m very bad at explaining things, so just ask if you didn’t get something.

1 Like

thanks but I try define another that check multiple for given array element and try to extend it more , but this is in result create extra or wrong line of code .
but I have done it now with another method