Why my program fails only for number 20?

Tell us what’s happening:
Describe your issue in detail here.
My question was “What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n?”
My code is simple I create a array to store all number from 1 to n and then I use ‘every’ method to test the condition.
it works well for all the numbers except 20! why?

  **Your code so far**

function smallestMult(n) {
let arr=[];
let c=0;
for(let a=1;a<=n;a++){
    arr.push(a);
}
//console.log(arr);
const divisible_test=(x)=>n%x==0;
while(c==0){
if(arr.every(divisible_test)){
c=1;
console.log(n)
return n;
}
n++;

}

}

smallestMult(5);
  **Your browser information:**

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

Challenge: Problem 5: Smallest multiple

Link to the challenge:

You might be timing out on bigger numbers? I would look for ways to speed up your code.

I would check if this is really the correct thing to do… if n is 20, why are you checking 21?

1 Like