This is the solution I’ve come up with. It works for all of the test cases, apart from the 4th one. I can’t really understand why, because the escape clause works for test cases that require more memory to run.

Could anyone help? Is it possible to tweak it somewhere that will get to run perfectly?

When I play around with the numbers, it works for many of them, but for ones like the 4th test case, it comes up saying that there’s a potential infinite loop. I could understand if the arguments were larger… but they’re not…

function smallestCommons(arr) {
arr.sort();
let number1 = arr[0];
let number2 = arr[1];
let i = number2 + 1;
let counter = [];
while (counter.length < 1) {
for (let p = number1; p < i; p++) {
if ((i % p == 0) && (p == number2)) {
counter.push(i);
console.log(i);
return i;
break;
} else if (i % p == 0) {
continue;
} else if (i % p != 0) {
i++;
break;
}
}
}
}
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/119.0.0.0 Safari/537.36

Challenge Information:

Intermediate Algorithm Scripting - Smallest Common Multiple

This doesn’t work as you expect for numbers. I would look at the MDN article on sort to see what I mean.

If that fixes it or not, I’d be curious if you could explain the general idea behind your approach? It looks unique and it’s not a common approach (or a representation I recognize) but it seems to work.

Thanks Jeremy. That fixes it. It still comes up with a possible infinite loop warning for some of the higher numbers, but it works, because all the test cases pass.

Yeah, when I’ve looked at other people’s solutions in some of these threads, I’m surprised that I haven’t seen anyone else post the approach I’ve used. Tbh I couldn’t think of any other way to do it.

I just started testing whether each number after the highest number passed is evenly divisible when divided by each of the numbers in the range between the smallest and the largest arguments passed into the function.

So I decided to use a while loop to test possible smallest common multiples, as I couldn’t say what the upper limit would be, using an escape clause to get out when the right conditions were met.

I just iterated over the range from the smallest to the largest argument again and again while increasing the integers tested as a potential smallest common multiple.

Could you, or anyone else, explain why the if statements don’t work for determining the variables? I’m new to JavaScript, but if I were to do the same thing in Python to determine variables, it would work fine.

Many thanks.

function smallestCommons(arr) {
// sorts arguments effectively
const [number1, number2] = arr.sort((a, b) => a - b);
// also sorts arguments effectively
// .....
// let number1 = Math.min(arr[0], arr[1]);
// let number2 = Math.max(arr[0], arr[1]);
// doesn't sort arguments effectively
// .....
// arr.sort();
// doesn't sort arguments effectively
// .....
// if (arr[0] < arr[1]) {
// const number1 = arr[0];
// const number2 = arr[1];
// } else {
// const number1 = arr[1];
// const number2 = arr[0];
// }
// Clause to escape while loop
let counter = [];
// Checking smallest common multiples by beginning from the first integer after the highest number passed as an argument
let i = number2 + 1;
while (counter.length < 1) {
// Count range between the numbers passed as arguments
for (let p = number1; p < i; p++) {
if ((i % p == 0) && (p == number2)) {
counter.push(i);
console.log(i);
return i;
break;
} else if (i % p == 0) {
continue;
} else if (i % p != 0) {
// this number has failed as a smallest common multiple, so trying the next number
i++;
break;
}
}
}
}
smallestCommons([2, 20]);
smallestCommons([23, 18]);
smallestCommons([4, 8]);