Smallest Common Multiple - Need Help

My function works for the first three tests, [1, 5], [5, 1], [1, 13], but for some reason it returns “undefined” for the last test [23, 18].
I have been stuck on this and can’t find the issue.
I am using a while loop that adds 1 to ‘i’ until ‘i’ is the SCM. The for loop in the while loop checks each int in between the smallest/biggest ints against the int ‘i’.

I will appreciate any help!

function smallestCommons(arr) {

  var smallest;
  var biggest;

  //check which int is bigger and place into vars big/small accordingly
  if (arr[0] > arr[1]) {
    biggest = arr[0];
    smallest = arr[1];
  } else {
    biggest = arr[1];
    smallest = arr[0];

  var multFound = false;
  var i = 2;
  var div = false;
  var mult;

  while(multFound == false) {

    //loops through all the integers between smallest and biggest, including both
    for (var k = smallest; k <= biggest; k++) {
      //if any of the ints aren't a div then break the loop
      if((i % k) !== 0) {
        div = false;
      div = true;

    //if at the end of the for loop, div is still true, then save that int as the smallest mult
    if(div) {
      mult = i;
      //end the while loop by telling it we found the mult
      multFound = true;
  return mult;
smallestCommons([23, 18]);

I tested your code on the FCC site and because your solution is taking a bit longer for that particular test, it thinks you may have an infinite loop. You can bypass this by putting:

// noprotect

on the first line of your code outside the function and it will pass the tests.

1 Like

Thank you, it works :smile: