Smallest Common Multiple Working But Not Passing

Smallest Common Multiple Working But Not Passing
0

#1

Hi!

I found a solution to the smallest common multiple challenge, but the grader says that none of the tests are passing. What am I doing wrong??

function gcd(arg1, arg2) {
  var div = 1;

  while (div > 0) {
    div = arg1 % arg2;
    if (div === 0) {
      return (arg2);
    } else {
      arg1 = arg2;
      arg2 = div;
    }

  }
  return (1);
}

function leastCommon(arg1, arg2) {
  var larger;
  var smaller;
  var divisor;

  // Check if either argument is equal to 1
  if (arg1 === 1) {
    return arg2;
  } else if (arg2 == 1) {
    return arg1;
  }

  // Make larger the larger of the two arguments
  if (arg1 > arg2) {
    larger = arg1;
    smaller = arg2;
  } else {
    larger = arg2;
    smaller = arg1;
  }

  // Get the greatest common divisor
  divisor = gcd(larger, smaller);
  return ((larger * smaller) / divisor);

}

function smallestCommon(arr) {

  var result;
  var numbers = [];
  var max;
  var min;

  // Set up the initial numbers array
  if (arr[0] > arr[1]) {
    max = arr[0];
    min = arr[1];
  } else {
    max = arr[1];
    min = arr[0];
  }

  for (var j = min; j <= max; j++) {
    numbers.push(j);
  }

  // Main Logic
  result = leastCommon(numbers[0], numbers[1]);
  for (var i = 2; i < numbers.length; ++i) {
    result = leastCommon(result, numbers[i]);
  }
  return (result);
}

console.log(smallestCommon([1, 5]));
console.log(smallestCommon([5, 1]));
console.log(smallestCommon([1, 13]));
console.log(smallestCommon([23, 18]));

#2

I tried your solution and all the tests pass. Try refreshing the page and running the tests again.


#3

Following your advice, I reloaded the page and tried again. It still fails. :face_with_raised_eyebrow:


#4

I have ran into something like this before. First, copy/paste your solution to an offline text file for safekeeping. Next, close all browser windows. Then, go back into the challenge and click the Reset your code button. Go back into your text file and copy/paste your solution back into the editor and run the tests. If it still does not work, then try Ctrl+F5 on Chrome to clear cache of page and try these same steps again. Let us know what you ended up doing if you do get it to pass. If what I suggest does not work, then let us know also.


#5

Okay, I feel a bit stupid right now. I renamed the main function to smallestCommon(), instead of smallestCommons(). After I added the s back to the function name, it all worked wonderfully.

Thank you so much for your help, @RandellDawson. I think I would have thrown my computer out the window otherwise. :smiley: