Challenge: Smallest Common Multiple - Fails

Can anyone explain why this code doesn’t work?

It fails on smallestCommons([23, 18]) despite correctly returning 6056820

I am using the largest number to increment.

I’ve read through other posts, where the issue is the infinite loop. What is the workaround for this?


function range(arr) {
  let rangeArr = [];
  arr.sort( (a, b) => a - b );
  let first = arr[0];
  let last = arr[1];
  for (let i = first; i <= last; i++) {
    rangeArr.push(i);
  }
  return rangeArr;
}

function checkDivisible(arr) {
  let largest = arr[arr.length-1];
  let divider = 0;
  let acc = 0;
  while (acc !== arr.length) {
    acc = 0;
    divider += largest;
    each(arr, function(element) {
      if (divider % element === 0) {
        acc += 1;
      }
    });
  }
  return divider;
}

function each(arr, func) {
  for (let i = 0; i < arr.length; i++) {
    func(arr[i], i, arr);
  }
}

function smallestCommons(arr) {
  let rangeArr = range(arr);
  return checkDivisible(rangeArr);
}

smallestCommons([23, 18])
``