Smallest Commons Algorithm

Tell us what’s happening:
Passing 4 of 6 tests but in the last two I keep getting “too much recursion”.
Any help is appreciated!

Your code so far


function smallestCommons(arr) {
let x, y;
let mult = 1;
//check which is larger and start multiplying
if(arr[0] > arr[1])
  {
    x = arr[0]
    y = arr[1]
    return checker(x, y, mult)
    // console.log(checker(x, y, mult))
  }
else
  {
    x = arr[1]
    y = arr[0]
    return checker(x, y, mult)
    // console.log(checker(x, y, mult))
  }
}


smallestCommons([1, 13])


//==========================================================
//feed 
function checker(x, y, z){
let check = x * z;
//use recursion with x and z, check with y
if (check % y === 0)
  {
    //check if divisible by numbers in between
    for (let i = y + 1; i < x; i++)
      {
        if(check % i !== 0)
          {
            // console.log(check)
            return checker(x, y, z+1)
          }
      }
      // console.log(x * z)
      return x * z;
  }
}

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0.

Challenge: Smallest Common Multiple

Link to the challenge:

So when i hit ure code
I get the next error: RangeError: Maximum call stack size exceeded

What it means is that somewhere in ure code, u are calling a function which in turn calls another function and so forth, until you hit the call stack limit.

This is almost always because of a recursive function with a base case that isn’t being met.

This also shows in ure code there is no base case whenever i try to run this on another IDE it crashes
Which is why u are getting an error

1 Like

So to my understanding, I am experiencing stack overflow? Could you elaborate more on the base case not being met and what is the general approach to fixing it?

Hi!

I have only a question!

Why don’t you use the algorithm of ‘least common multiple’ and apply it?
Sure there are other possibilities but that one is easy to understand and leads to the expected solution!