Javascript Doubt


Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.

Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.


function smallestCommons(arr) {
  let numbers = []
  for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {

  let n = numbers[numbers.length-1];

  while (true) {
    if ( => n%item)).reduce((a,b) => a+b) == 0) {
      return n;
    else {


I get all the expected answers, but I could not get that:

smallestCommons([23, 18]) should return 6056820.

Any thoughts?

Thanks in advance!

Your code will produce the correct answer so that logic is sound but…

Your algorithm makes many iterations to get that correct answer so you are probably tripping the infinite loop protection in the FCC environment. (Also, repeating that complex chained if condition every time is “expensive” in that those methods iterate too).

The key to solving this efficiently (and not tripping infinite loop protection) is not testing some of the numbers that could not possibly be the SCM. Currently your while loop executes about as many times as the answer. 6056820 is a lot of looping.

Just as a hint… For instance, in the case of [1,5] the SCM would have to be some multiple of 5 so checking 6 or 22, or 37 or anything that was not a multiple of 5 would all be wasted iterations. There are more efficiencies to be had but that is a good start.

Just telling you up front this one can take some time to solve. Don’t get discouraged.

Thanks man!

I worked doing that:

n += numbers[numbers.length-1];

You’re welcome!