Smallest Common Multiple - correct answer not being accepted

Smallest Common Multiple - correct answer not being accepted
0

#1

Tell us what’s happening:
I’ve confirmed in my own browser that the required values are output by the function, but pressing the “Run the Tests” button does nothing (doesn’t even result in any error messages).

I’m using EC6 things like the function arrow and let/const, but that should be OK, shouldn’t it?

Your code so far


// Intermediate Algorithm Scripting: Smallest Common Multiple
function smallestCommons(arr) {
  // Store values.
  arr = [...arr].sort((a,b) => a > b);
  // Generate array of divisors.
  let divs = [];
  for (let i = arr[0]; i <= arr[1]; i++) 
    divs.push(i);
  // Determine highest possible multiple = all numbers multipled together.
  const hpm = divs.reduce((a, b) => a * b);
  // Test for whether a number is divisible by all divisors.
  function divisibleByAll(div) {
    var ok = true;
    for (let c = 0; c < divs.length; c++) {
      if (div % divs[c] != 0) {
        ok = false;
        break;
      }
    }
    if (ok) {
      return true;
    } else {
      return false;
    }
  }
  // Iterate through nums from the largest divisor through the highest possible.
  for (let i = arr[1]; i <= hpm; i += arr[1])
    if (divisibleByAll(i)) return i;
}

smallestCommons([1,5]);

Here are some browser JS errors that the FCC libraries throw:

TypeError: "unknown: o is undefined"
gethttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:32:10856hhttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:49:1470ihttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:21:4737rhttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:49:11676ohttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1367ihttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1090ahttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1758visitQueuehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:29396visitMultiplehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:28845visithttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:29798nodehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:31:17426ahttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1903 transformers.js:85:6

TypeError: "unknown: o is undefined"
gethttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:32:10856hhttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:49:1470ihttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:21:4737rhttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:49:11676ohttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1367ihttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1090ahttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1758visitQueuehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:29396visitMultiplehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:28845visithttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:47:29798nodehttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:31:17426ahttps://learn.freecodecamp.org/commons-b48a099b457b77b5ddc8.js:48:1903 transformers.js:85:6 ```

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:61.0) Gecko/20100101 Firefox/61.0.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple


#2

Hi, could you please explain the math behind your code, I tried to follow but It’s kind of confusing.
BTW, I made some change to your code to make it more concise:

since ok is a boolean variable, you can simply do:

return ok;

#3

Actually, if you really want to make the divisibleByAll function more concise, there is no need for the ok variable at all.

  function divisibleByAll(div) {
    for (let c in divs) {
      if (div % divs[c] != 0) {
        return false;
      }
    }
    return true;
  }

@globewalldesk The reason your code will not run is because the linter used by FCC expects you to use { } for the for loop code block. So instead of writing:

  for (let i = arr[1]; i <= hpm; i += arr[1])
    if (divisibleByAll(i)) return i;

you should be writing:

  for (let i = arr[1]; i <= hpm; i += arr[1]) {
    if (divisibleByAll(i)) return i;
  }

It was probably the fact the both the for loop and the if statement do not use { }.


#4

Sorry for the slow reply. Thanks for return ok; Duh!

How the math works? Well, I didn’t think too hard about it so it probably isn’t the most efficient algorithm, but after generating the array of divisors (so the arguments 2, 5 generate the divisors array [2, 3, 4, 5]), then the question is: what is the smallest number that has all members of the array as divisors (i.e., that is a multiple of all members; that is the smallest common multiple)?

My strategy is to come up with a list of possible answers and then go through them from smallest to largest, stopping when a multiple of all members is spotted (it will be the smallest common multiple). Since the the answer must be a multiple of the largest of the divisors, it follows that, when generating a brief list of possible answers (that will contain the SCM), there is no need to consider any divisors other than the largest divisor. Might as well just consider multiples of the largest divisor, because that will be the shortest list that contains the SCM; e.g., in the case of [2, 3, 4, 5], consider multiples of 5, i.e., count by 5s. The strategy then is to consider each multiple of 5 (from smallest to largest) and see if it is divisible by all of the other divisors. If not, go on. If it is, stop and return that number since it is the SCM.

By the way, I don’t think it’s really necessary but I calculated the largest possibility we might need to consider, which is definitely a common multiple, namely, the sum of all the divisors. In my for loop I make this the upper bound; but again, this wasn’t really necessary…


#5

That’s another “duh” there–yes, I could just return false right within the if statement.

Thanks for the explanation about the linter. Yes, it works now!