Won't this reset to zero every time?

One solution was:

function smallestCommons(arr) {
  // Setup
  const [min, max] = arr.sort((a, b) => a - b);
  const numberDivisors = max - min + 1;
  // Largest possible value for SCM
  let upperBound = 1;
  for (let i = min; i <= max; i++) {
    upperBound *= i;
  }
  // Test all multiples of 'max'
  for (let multiple = max; multiple <= upperBound; multiple += max) {
    // Check if every value in range divides 'multiple'
    let divisorCount = 0;
    for (let i = min; i <= max; i++) {
      // Count divisors
      if (multiple % i === 0) {
        divisorCount += 1;
      }
    }
    if (divisorCount === numberDivisors) {
      return multiple;
    }
  }
}

smallestCommons([1, 5]);

Every time the inner for loop completes and goes back up to the outer for loop it looks like it sets divisourCount to zero again so divisor will never equal numberDivisors.

Yes, it resets to zero each time and re-counts the number of divisors. For each new multiple it doesn’t matter how many divisors the last multiple had.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.