Intermediate Algorithm Scripting - Smallest Common Multiple

Tell us what’s happening:
Firstly, so you can see where from I am looking at this challenge.
The smallest common multiple is something I have never heard of. So if anyone has any easy to understand sources to familiarize myself with the idea of smallest common multiple I would be more than happy to see it. Wikipedias formulas are way out of my league. All the sites I have found are really confusing with their explanations.

For the challenge itself this is what I’ve gotten out of it:

I need to make sure I fill the array with numbers between the numbers given in the argument array.
Then all of those said numbers are used to do something to get the smallest common multiple.
In the end I should return the smallest common multiple.

So essentially I’m missing the core knowledge needed in the challenge and I need something to get a grasp of the maths here.

Your code so far

function smallestCommons(arr) {
  return arr;


Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Link to the challenge:

The smallest common multiple is a basic math concept.

Do you understand what a multiple is?

The multiples of 2 are: 2, 4, 6, 8, 10, 12, …
The multiples of 5 are: 5, 10, 15, 20, 25, …

The smallest common multiple is the lowest number that both sets of multiples have. In this case, that is 10.

Ok, the Smallest (or Least) Common Multiple of two (or more) numbers is simply the smallest number which is a multiple of each of those numbers.
For instance, the SCM of 4 and 6 is 12, because 12 is the smallest number which can be divided (with no remainder) by both 4 and 6.

However, for this challenge, there is a slight complication in that it specifies that your SCM should be divisible by both numbers in the array AND also every number in-between. The initial array may also not be in numerical order.

HINT: The SCM of any two consecutive numbers is the product of those two numbers (i.e. SCM of 5 and 6 is 5*6 = 30). So, when dealing with a range of numbers, the minimum possible value of the SCM must be the product of the two highest values in the range.

The wording is a bit odd, but they are asking you to find the SCM of a range. So, if your range it 2 to 4, what is the SCM or 2, 3, and 4

2, 4, 6, 8, 10, 12, 14, 16, 18, 20, …
3, 6, 9, 12, 15, 18, 21, …
4, 8, 12, 16, 20 …

What is the lowest number that is in all three lists?

Answer: 12

Another way to think of it is: What is the lowest number that is a multiple of all three numbers?

You don’t need a lot of math to be a programmer for most things, especially frontend web development (in most cases). But you will be expected to understand certain basic concepts. I always say that by middle school, you’ve probably learned enough math. If you are unfamiliar with terms like these, you may want to review a little, on the side. You don’t have to do really complex stuff, but some of these things will be needed. In some cases it may just be reviewing the terminology.

1 Like

Reading the answers here and googling a ton has taken me this far:

function smallestCommons(arr) {
  //sort the array and add values between [0] and [1] into array 
  let newArr = arr.sort((a,b) => a - b);
  for (let i = newArr[0] + 1; i < newArr[newArr.length - 1]; i++) {
    newArr.splice(-1, 0, i)

  //find the lowest and highest value in newArr and declare a variable for factorial of array items
  let lowestInNewArr = newArr[0];
  let highestInNewArr = newArr[newArr.length - 1];
  let factorial = 1;

  for (let m = 0; m < newArr.length; m++) {
    factorial *= newArr[m]

  //check that variables work as intended
  //declare a variable for result to be returned
  let result = 0;

  for (let j = highestInNewArr; j < factorial; j++) {
    if ( => j % element === 0)) {
      result += j
  return result;


I can’t seem to find a way to find the modulo of j with every item in array. Any tips on that?

This made me think of using the factorial of numbers in array as the highest value to act as a boundary but my last loop still warns me of infinite loop.

This doesn’t seem to work as minimum of [18,23] is 6056820 not 506.

Can you explain what you are doing here?

I can see what the code literally does, but I want to know what you are trying to get this block to do.

I’m adding one to highestInNewArr as long as j is less than the factorial and in every pass I’m trying to check if j has a modulo of 0 with every number in array.
If it does, add j to result and exit the loop.

After that, return result.

E: fixed wording

Why add j to the result?

Why are you using a map? A map is not a general purpose loop, its a method to make a brand new array of the exact same length out of an old array.

I’m adding j to result so I have the SCM to return.

Map is the latest I tried. It seems I haven’t understood .map(); correctly.
I also tried forEach(); but I couldn’t get it to work either. As far as I know my function after the arrow should be working.

Ok, but why not just set the result to j. Or just return j?

What does a forEach() do? Array methods are cool but you have to know which one to use. What do you want this array method to do?

Almost, its very close

Didn’t even cross my mind even though nearly every challenge does this now that I came to think of it.

According to MDN it executes the given function once for each array element.
Reading this over and over again just made me think that does it mean it executes the given function arr.length times to each item in array?

Yeah, it executes the function, but it doesn’t do anything with the result. What do you want to do with the result of that callback function?

But does it execute the function as many times as there are items in the array?
Oh, it discards the return value. So it discards it instantly and the result wont get a value as true or false?

As I’m using if-statement, doesn’t it value the if part true and else part executes only when the first part is false.
Also, as I left out the else part doesn’t if-statement still check that the first part should equal to true but just continue on with the code disregarding the else part.
See under for “clarification” :joy:

if (true) {
execute code here
} else {
execute code here instead

//is the same as

if (true) {

But your if statement is never true, because nothing happens with those return values of the callback.

Are you thinking of this method:

That did it! Thank you so much!

But I still wonder why does the challenge warn me of a possible infinite loop. As I can tell there is nothing that could loop infinitely as all the loose ends are blocked with boundaries.

The infinite loop warning happens any time your code is slow

Glad you solved it in the end! Sorry I was afk for a while!
I didn’t explain myself very well but the minimum possible SCM for a range of consecutive integers can be inferred as the product of the two highest in the range, but it doesn’t mean that it is the true minimum SCM, only a lower bound (an upper bound being the product of all of the numbers in the range). More accurately, the SCM is the product of the two highest integers, or a multiple thereof.

You owe me no apologies. You helped me out of kindness when you didn’t have to.
I’m very thankful for it!

Your new explanation is even better and easier to understand.

Thank you for all of your time and effort!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.