smallestCommon

by trentHarlem

HTML

<p>
For example, if given 1 and 3, <br>
find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 And 3.<br>
The answer here would be 6.
</p>

JavaScript

function smallestCommons(arr) { 
  let max = Math.max(...arr);
  let min = Math.min(...arr);
  let multple = max;
  for(let i = max; i >= min; i--){
  //console.log(multple,i,max,min,multple%i===0)
    if(multple % i !== 0){
      multple += max; 
      i = max;
    } 
  }
  //console.log(multple)
  return multple;  
}




/* function smallestCommons(arr) {
  let min = Math.min(...arr)
  let max = Math.max(...arr)
  //console.log(min, max)
  const minArr = []
  const maxArr = []
  for (let i = min; i < max; i++) {
    maxArr.push(i * max)
    minArr.push(i * min)
    }
  console.log(maxArr)
  console.log(minArr)
  console.log(maxArr.filter(el=>minArr.includes(el)))  
  return maxArr.reduce((a,c)=> c,0);
} */
//const smallestCommonsArr = arr => arr.reduce((a,c,i)=>a)

smallestCommons([1, 3]) // should return 6.
smallestCommons([1, 5]) // should return 60.
smallestCommons([2, 10]) // should return 2520.