Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

array

function smallestCommons(arr) {
    const [min, max] = arr.sort((a, b) => a - b);
    console.log([min])
  const numberDivisors = max - min + 1;
  console.log(numberDivisors)
  // Largest possible value for SCM
  let upperBound = 1;
  for (let i = min; i <= max; i++) {
    upperBound *= i;
  }console.log(upperBound)
  // 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;
      }
    }console.log(multiple)
    if (divisorCount === numberDivisors) {
      return multiple;
      
    }
  }
}
Source by www.freecodecamp.org #
 
PREVIOUS NEXT
Tagged: #array
ADD COMMENT
Topic
Name
2+1 =