Intermediate Algorithm Scripting: Smallest Common Multiple_Help Requested

Hello. I’m working on the Smallest Common Multiple Challenge. https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple
I would like to write a recursive function to replace:

for (let i = 0; i < fullArr.length; i++) {
      if (multArr % fullArr[i] === 0) {
      return multArr / 2;
      } else {
        return multArr * 2;
      }
    }

in my code. Wondering if I’m on the right track with my code. I’ve cleared the first three tests. Thanks in advance!

function smallestCommons(arr) {
  let newArr = arr.sort(function(a,b) {return a-b});
  console.log (newArr);

  let i = newArr[0];
  console.log(i);

  let fullArr = [];
  while (i <= newArr[1]) {
    fullArr.push(i);
    i++;
  }
  console.log(fullArr);

  let multArr = fullArr.reduce(function(a,b) {
    return a * b;
  })
  console.log(multArr);

    for (let i = 0; i < fullArr.length; i++) {
      if (multArr % fullArr[i] === 0) {
      return multArr / 2;
      } else {
        return multArr * 2;
      }
    }
}


smallestCommons([1,5]);

Hi! I like to start every post by saying I’m no expert. If you’ve solved it by now, then just ignore me! :sweat_smile: Anyway, it looks to me like you should narrow your search for the smallest common multiple. Using your solution for smallestCommons([2, 10]) and console logging multArr and fullArr[i] in your last loop, your solution divides 3628800 by 2 only and stops. (Your solution getting 1814400 compared to the answer 2520.)

I tested it like this…

function smallestCommons(arr) {
  let newArr = arr.sort(function(a,b) {return a-b});
  console.log (newArr);

  let i = newArr[0];
  console.log(i);

  let fullArr = [];
  while (i <= newArr[1]) {
    fullArr.push(i);
    i++;
  }
  console.log(fullArr);

  let multArr = fullArr.reduce(function(a,b) {
    return a * b;
  })
  console.log(multArr);

    for (let i = 0; i < fullArr.length; i++) {
      console.log(multArr, fullArr[i]); //<-----------------here
      if (multArr % fullArr[i] === 0) {
      return multArr / 2;
      } else {
        return multArr * 2;
      }
    }
}

smallestCommons([2,10]);

In case you’re interested… What worked for me was using a loop that was quite meticulous, basically going through each number possible until my repl.it crashes for the last problem (but works on brackets and google chrome.) My first solution was sadly not accepted by FCC though (for intensive looping I think) so I tweaked it a little bit in my second solution and solved the problems. Good luck. ^^

I hate to be a bummer but I don’t think you are going in the right direction with that algorithm. The first three tests are all variations on the same set of numbers and I think it is just coincidence that you passed that set.

Yeah, I went ahead and looked at the answer. Thanks for the reply.