Intermediate Algorithm Scripting - Smallest Common Multiple

Tell us what’s happening:
All of the other tests have confirmed the code is correct, but the last one (smallestCommons([23, 18])) doesnt have enough memory to complete, returning ‘RangeError: Maximum call stack size exceeded’.

Any tips on how to solve this?

Your code so far

function smallestCommons(arr) {
  arr.sort(function(a,b) {
    return a - b
  })
  let scm = arr[0]
  for (let i = arr[0]; i <= arr[1]; i++) {
    scm = (scm * i) / gcd(scm, i)
  }
  return scm
}

console.log(smallestCommons([23, 18]));

function gcd(a,b) {
  if (a > b) {
    return gcd(a-b, b);
  } else if (a < b) {
    return gcd(a, b-a);
  } else {
    return a
  }
}

Your browser information:

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

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Link to the challenge:

I don’t think this is quite the right formula. Id double check Wikipedia.

At the bottom of the article, under Other.

Edit - no, that part is lining up (though no need to do arr[0] * arr[0])

Ahh! This is slower than the typical algorithm.

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