Smallest Common Multiple appears to have a whitespace related bug

Tell us what’s happening:
The exercise, Intermediate Algorithm Scripting: Smallest Common Multiple, states that “Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.”

All but the last test pass. The last test is for smallestCommons([23, 18]) which should return 6056820.

Every time I adjust whitespace, a different value is logged. Clearly, I suspect a bug.

Your code so far
function smallestCommons(arr) {
let smallest = 1;

arr = arr.slice().sort((a, b) => a > b ? 1 : -1);

let range = [];

for (let i = arr[0]; i <= arr[1]; i++) {
range.push(i);
}

while(range.some(e => {
return smallest % e;
})) {
smallest++;
}
console.log(smallest);

return smallest;
}

smallestCommons([23, 18]);


function smallestCommons(arr) {
  let smallest = 1;

  arr = arr.slice().sort((a, b) => a > b ? 1 : -1);

  let range = [];

  for (let i = arr[0]; i <= arr[1]; i++) {
    range.push(i);
  }
  
  while(range.some(e => {
    return smallest % e;
  })) {
    smallest++;
  }
  console.log(smallest);

  return smallest;
}

smallestCommons([23, 18]);

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

The white space doesn’t matter
It seems you are triggering the infinite loop protection, it stops the function prematurely because it is needing too much time
You need to find an algorithm that needs less steps

1 Like

I used multiples of the largest number, and that stopped the infinite loop protection.