Smallest common multiple bug#

Hello, I was solving this smallest common multiple challenge in the intermediate algorithm challenges and I went with following approach

function smallestCommons(arr) {
  const small = Math.min(...arr),
        big = Math.max(...arr);

  if (small === 0 || big === 0) throw new Error('Dividing with 0 creates black holes');

  let bool;

  for (let i = 1; true; i++) {
    bool = true;
    for (let j = small; j <= big; j++) {
      if (i % j !== 0) {
        bool = false;
        break;
      }
    }

    if (bool) {
      return i;
    }
  }
}

it passes first three tests, but cannot pass last two even tho it returns the expected answer, then I searched for other solutions and somehow they pass all of the

For example following solution isn’t mine and passes just fine

function smallestCommons2(arr) {
  // Sort array from greater to lowest
  // This line of code was from Adam Doyle (http://github.com/Adoyle2014)
  arr.sort(function(a, b) {
    return b - a;
  });

  // Create new array and add all values from greater to smaller from the
  // original array.
  var newArr = [];
  for (var i = arr[0]; i >= arr[1]; i--) {
    newArr.push(i);
  }

  // Variables needed declared outside the loops.
  var quot = 0;
  var loop = 1;
  var n;

  // Run code while n is not the same as the array length.
  do {
    quot = newArr[0] * loop * newArr[1];
    for (n = 2; n < newArr.length; n++) {
      if (quot % newArr[n] !== 0) {
        break;
      }
    }

    loop++;
  } while (n !== newArr.length);

  return quot;
}

Link to challenge

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

This challenge has a VERY strict time limit, so your code needs to be very efficient to pass.

Currently, when an iteration fails in your code you only increment by one:

for (let i = 1; true; i++)

You should look to increase by a larger valid amount. Also, to further increase efficiency, the i value should start from the ‘big’ value.

1 Like

That makes sense. Thank you

Hello,

Same issue - different code. Also I do not pass only the last test. Can you please have a look and advice me ?

  function smallestCommons(arr) {

let orderedArr = [Math.min(arr[0], arr[1]), Math.max(arr[0], arr[1])];
let num = 1;
let truthArr = [];

function thruthCheck(val) { return val === true };

while (true) {
 truthArr = [];
  if (num % arr[0] === 0) {

  if (num % arr[1] === 0) {

    for (let i = orderedArr[0] + 1; i < orderedArr[1]; i += 1) {

      if (num % i === 0) {


        truthArr.push(true);
      } else {
        truthArr.push(false);
      }

    }

  }

}

if (truthArr.every(thruthCheck) && truthArr.length > 0) {
  return num;
  break;
}

num++;
 }



}


 smallestCommons([23, 18]);

For number 15, you can get following common multiples 1, 3, 5

5 * 3 = 15 => 15 = 15
1 * 5 = 5 =>  5 + 5 + 5 = 15
3 * 1 = 3 => 3 + 3 + 3 + 3 + 3 = 15

so do not check every number one by one

For example, for the tests if you go like this the number will get bigger quicker than incrementing one by one.

[1, 5] =>  5 * 4 = 20 => 20 + 20 + 20 = 60
[1, 10] => 10 * 9 = 90 => 90 + 90 + 90 + .... + 90 = 90 * 28 = 2520

Thank you! I will give it a try.