Passing all cases BUT the LAST one: Smallest Common Multiple

Hey fellow campers. In need of a tiny (I hope…) nudge on the Smallest Common Multiple challenge. I’m able to pass all tests cases except the final one with the code below:

function smallestCommons(arr) {
  var containerArr = [],
    //find min and max values of arr//
    arrMin = Math.min.apply(null, arr),
    arrMax = Math.max.apply(null, arr),
    commonDivisors,
    upperBound,
    lowerBound;

  //create array of nums between max and min of arr
  for (var i = arrMin + 1; i < arrMax; i++) {
    containerArr.push(i);
  }


  commonDivisors = function(arr, num) {
    var _divisibleByAll,
      _iterator = 0,
      _currentNum,
      _condition = false;

    while (!_condition) {
      _iterator++;
      _currentNum = num * _iterator;
      _divisibleByAll = arr.every(function(item) {
        return _currentNum % item === 0;
      });
      if (_divisibleByAll) {
        break;
      }
    }
    return _currentNum;
  };

 //find common multiples of arrMax and arrMin

  upperBound = commonDivisors(containerArr, arrMax);
  lowerBound = commonDivisors(containerArr, arrMin);

 //return the larger of the two common multiples.
  return upperBound;
}


smallestCommons([1,5]);

A couple of thoughts:

  • The above code assumes that the first number that is divisible by all numbers in the range is the smallest number that is divisible by both numbers in the range evenly: this isn’t true since I’d be passing all cases otherwise.
  • The code at this stage is more right than wrong (since I have all but one of the test cases passed) but there is an edge case that I’m not seeing.

Any hints or nudges would be greatly appreciated as I feel I’m almost there! I’ve been working on this all day and I think I’m a little exhausted for now. Thanks in advance!

Sean

The variable containerArr doesn’t include the endpoints. In the last test case (arr = [23, 18]), the number your function returns is a multiple of 19, 20, 21, 22, and 23, but it is NOT a multiple of the lower endpoint (18).

Agatha! You’re AWESOME. It was one of those pesky ‘off by one’ errors that I just could’t see cause I’d been looking at things so long. Thanks for catching that. Be well.