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;
}
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/108.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

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.

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.

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
console.log(lowestInNewArr)
console.log(highestInNewArr)
console.log(factorial)
//declare a variable for result to be returned
let result = 0;
for (let j = highestInNewArr; j < factorial; j++) {
if (newArr.map(element => j % element === 0)) {
result += j
break;
}
}
return result;
}
console.log(smallestCommons([23,18]));

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.

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.

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.

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?

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”

if (true) {
execute code here
} else {
execute code here instead
}
//is the same as
if (true) {
}

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.

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.