Smallest common multiple...challenge

Smallest common multiple...challenge
0.0 0

#1

Here is another one im kind of stuck on…

Here is the code:

function smallestCommons(arr) {
  var min = Math.min(arr[0], arr[1]);
   var max = Math.max(arr[0], arr[1]);
  var range =[];
  
  for(var i = start; i <= end; i++){
    range.push(i);
  }
  var LCM = 0,
      flag = true;
  
  while(flag){
    
    LCM++;
  
  
  for(var j = start; j <= end; j++) {
   
    if (LCM % j !==0){
      break;
    }
  else if(j===end) {
  flas = false;
}
  }
 
 }
return LCM;
}

smallestCommons([1,5]);

Since LCM increases by 1 at the beginning, and j also increases 1 at the beginning, that means its
LCM % j !==0
1 % 1 !==0…<-- This is 0, right?


#2

Do you understand in words what you’ve written here? As in could you explain how you’re attempting to solve the problem? Also as a side note, start and end don’t seem to be defined here, did you mean min and max?

Also out of interest, why have you made a range array if you never use it?

For the actual question asked, yes 1 % 1 is indeed 0. Do you understand why the % operator is being used?

I’m aware the above questions I’ve asked might come across as harsh, that’s not what I’m intending - actually the problems from the point you’re at now become more about problem solving skills than implementing the code itself, and understanding “in words” is actually in my view somewhat the point of the exercises.


#3

Where do you assign a value to the variable called start?
And also, n.b. a much cleaner way of solving this is to note that
lcm(a,b) = a*b/gcd(a,b) and that lcm(a,b,c) = lcm(lcm(a,b),c)


#4

This is the challenge ive seen elsewhere, i did not write it myself, but i want to understand it as much as possible, and i do know that range array was unnecessary, Im just concerned about my question about %, and yes i understand how % works, but in this case, i dont get it


#5

alright, let’s talk about what the function is intending to do. (in words)

we want to find the smallest common multiple of all the numbers between two numbers.

to do so we have a guess for the smallest common multiple, which we start as small as possible (we guess at first the number 1 for this).

We then test if that candidate is indeed a common multiple, that is we test if our guess is a multiple of every number in the range.

If it’s not a multiple of all of them, we increase our guess by 1, and try again.

Once we find a number that is a multiple of all of the numbers in the range, we have the smallest common multiple because it was the first number that we tried that was a common multiple.


okay with that said, how do we test if a number is a multiple of another number?

By finding the remainder of a division. a % b returns the remainder when a is divided by b. If there is no remainder, aka a % b = 0, then a is a multiple of b.