Intermediate Algorithm Scripting - Smallest Common Multiple

So, I have a couple questions on this challenge. The first one is that my current algorithm seems to output weird numbers for higher output values, but when I try the same exact algorithm on a different IDE like JSFiddle, it seems to output the correct values, so I am not sure why this doesn’t work.

Current Code:

function smallestCommons(arr) {
  let multiple = arr[0];
  // Finds multiple
  for (let i = 1; !mult(multiple, arr[0], arr[1]); i++) {
    multiple = arr[0] * i;

function mult(multiple, num1, num2) {
  let array = [num1, num2];
  array = array.sort((a, b) => {return a - b});

  for (let j = array[0]; j <= array[1]; j++) {
    if (multiple % j !== 0) {
      return false;
  return true;

smallestCommons([5, 1]);
smallestCommons([2, 10]);
smallestCommons([1, 13]);
smallestCommons([23, 18]);

Another interesting thing also came up before. In my former version of this code, my for loop looked like this:

 for (let i = 1; multiple % arr[1] !== 0 && !mult(multiple, arr[0], arr[1]); i++) {
    multiple = arr[0] * i;

I know the first part of the condition in the for loop is redundant, but when I run the code (same thing happened on JSFiddle), it seems to output 5 as the multiple. I am not sure why this is the case, and am curious. Any help is very appreciated!

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

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Link to the challenge:

I would suspect that if you try the failing test case, you get a warning about an infinite loop. You are increasing i slower than you need to. Also your initial guess is too small. If you think a little bit about those two values, you can significantly speed up your code. Let me know if you need more of a hint.

1 Like


So good news, I did solve it. However, I may have done it a bit differently than you recommended. This is my for loop now:

function smallestCommons(arr) {
  let multiple = 0;
  // Finds multiple
  for (let i = arr[0] * arr[1]; !mult(multiple, arr[0], arr[1]);) 
    multiple += i;
  return multiple;

And then I added this condition at the beginning of my mult function:

 if (multiple === 0)
    return false;

Out of curiosity, what were you implying that I should have done? Also, do you mind answering my question about my first version of my for loop with multiple conditions (hopefully, that makes sense)?

Thanks for the help once again!

This is a bad for loop. I see why you did but this isn’t how a for loop should be used.

I’m not sure your code would work in general.

For the approach in your OP, I would have increased by max


I understand that the for loop was not the best, and to be honest I was surprised it worked (I should have used a while loop instead) in general. However, I don’t know what you mean by max. Can you clarify please?



It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

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