Intermediate Algorithm Scripting - Smallest Common Multiple

Tell us what’s happening:
Please can someone explain why my while loop does not evaluate to false

Your code so far

function smallestCommons(arr) {
  const min = Math.min(...arr);
  const max = Math.max(...arr);
  const array = [];
  let count = 1;
  for (let i = min; i <= max; i++) {
    array.push(i);
  }
  const dup = array.slice(); // copy array to dup

  while (dup.every((elem) => dup[dup.length - 1] % elem === 0) == false) {
    for (let i = 0; i < array.length; i++) {
      dup[i] = array[i] * count
    }
    console.log(dup)
    count++
    
      
    
  }

  return dup[dup.length - 1];
}


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Link to the challenge:

Try to put a console.log() line before your return statement, it helps to see what will be returned.

And i see this .

Potential infinite loop detected on line 11. Tests may fail if this is not changed.

Thanks but i would like to understand why there is an infinite loop. I do not understand why the while loop is not working as expected

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