I am trying to build a function to find the smallest common multiple by numbers in range. For example if i pass smallestCommons([1,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.
I use the recursion to find the Smallest Common Multiple, but if i pass the second index of array with number more than 12 than it will output maximum call stack exceeded
function smallestCommons(arr) {
arr.sort((a,b) => b - a);
let newArr = [];
for(let i = arr[0]; i >= arr[1]; i--) {
newArr.push(i);
}
function findCommon(multiples) {
let currentValue = newArr[0] * multiples;
if(newArr.every(item => currentValue % item == 0)) {
return currentValue;
} else {
return findCommon(multiples + 1);
}
}
return findCommon(1);
}
console.log(smallestCommons([1,13]));
console.log()at the end there. Could you provide the current output and any errors in the console? - Axel Köhler