Smallest Common Multiple - Maximum call stack size exceeded

Tell us what’s happening:
Can you help me avoid reaching the max call stack?
When I log to the console it seems to be exceeding the maximum call stack size.
I have an if statement checking that every item in the array meets the correct condition else j counting up by 1 each time and recusing.
I can pass all tests except the last two which need to reach higher numbers than the console is allowing. I also tried adding the highest number in the array to J to count up faster but it still isn’t arriving before max is reached.

// running tests
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
// tests completed

Your code so far


function smallestCommons(arr) {
arr.sort(function(a, b){return a-b});
let array = [];
for (let i = arr[0]; i <= arr[1]; i++){
  array.push(i)
}
let j = 1
function checkMult(arr) {
  if (arr.every( test  => (Number.isInteger(j / test)))) {
    //console.log(j);
    return j
  } else {
      j++;
      checkMult(array);
    }
}
checkMult(array);
//console.log(j);
return j;
}

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/87.0.4280.88 Safari/537.36.

Challenge: Smallest Common Multiple

Link to the challenge:

this is because there is too much recursion - it is not a time thing here, there are too many function calls waiting to complete and there is no memory for others - maybe a few thousands function is too much there

instead of going brute force, you could check wikipedia for an algorithm and translate one of those to JavaScript

You could use a while loop instead of recursion.

Thanks both of you.

I switched to the while loop since I already did all the work.

@ilenia thanks for reminding me that it’s ok to google an algorithm and rewrite it instead of creating a whole new way myself.