Replace static for loop

Tell us what’s happening:

I really don’t like that static number inside my for-loop and have no idea of a work around with a while loop. Can anybody show me a simple way how to get rid of the static value?

  **Your code so far**

function smallestCommons(arr) {
let sm = Math.min(...arr);
let bg = Math.max(...arr);
var arrCheck = []
for (let i = sm; i<bg+1;i++){
  arrCheck.push(i)
}
for (let j = 2; j < 100000000;j++){  
    var arr2 = arrCheck.map(x => j % x)
  if (arr2.every(elem => elem == 0)){
    console.log(j)
    return j
  }
  }
}


smallestCommons([1,5]);
  **Your browser information:**

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

Challenge: Smallest Common Multiple

Link to the challenge:

What exactly do you mean by static number inside your for loop?

The 10000000 which I had to use to get through all tests

You can try two things.

First, you can make a better upper bound by making a big number that you know is divisible by every number in the range.

Second, you can replace the slow loop based approach with an approach based on the Wikipedia articles about GCD and LCM.

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