Smallest Common Multiple using Recursion Help?

Tell us what’s happening:
I’m trying to solve the lowest common multiple challenge. After some research and testing, I think solving it with recursion might work. What I’m trying to achieve with my code is to loop through the range and if each number is evenly divisible by a multiple of the max then return that number.

If not, the idea is to reset the counter, and add the max value to the max (max += max) - then call itself until the count is equal to all the values in the range which would be hence be the lowest common multiple.

I feel as if the logic makes sense but I’m having a really hard time trying to implement the recursion as I’ll admit I’m not very good at it. Especially considering that the recursive function has to be inside the smallestCommons functions and it has to call itself outside of the loop.

If anyone could offer any advice or suggest how to structure the code would be greatly appreciated.

  **Your code so far**

function smallestCommons(arr) {
let count = 1;
let test = max;
let max = Math.max(arr[0], arr[1]);
const min = Math.min(arr[0], arr[1]);
let range = [];
for (let i = min; i <= max; i++) {
  range.push(i)
}

function isDivisible(test) {
for (let i = 0; i < range.length; i++) {
  if(range[i] % test === 0) {
    count ++;
  }
}
if (count === range.length) {
  return test
}  else  {
  test += test;
  isDivisible(test);
}
}
}

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/99.0.4844.51 Safari/537.36

Challenge: Smallest Common Multiple

Link to the challenge:

For this to work you would need to call the recursive function inside of your function.

But ttis approach probably will take too long for the tests… I would pause and research a more straightforward solution. The wikipedia page for Least Common Multiple has some good algorithms for this problem.

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