# Smallest Common Multiple - code works when using node, but not when running test

Smallest Common Multiple - code works when using node, but not when running test
0

Tell us what’s happening:
When I run this via node. This code works just fine, but it seems to fails when running the test. I have noticed this has happened with some other things as well. Is this due to a memory restriction?

Your code so far

``````
function smallestCommons(arr) {
arr = arr.sort((a,b) => a - b)
let num=arr[0]
let complete = true
let i = 1

while(complete){
i++
num=arr[0]*i
complete = doIt(arr,num)
}
console.log(num)
return num;
}

function doIt(arr, num) {
let alpha=arr[0]

while(num%alpha==0 && alpha<=arr[1]){
alpha++
}
if (alpha>arr[1]){
return false
} else {
return true
}
}

smallestCommons([23, 18]);
``````

Your browser information:

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

it is more a time restriction - your code takes too much time to run

the freecodecamp has an infinite loop protection that will stop the code from running after a certain amount of time has passed. You need a faster algorithm, one that doesn’t check the numbers one by one, as that would mean 6 millions iterations, which takes too much time and the infinite loop protection kicks in

Thanks, gave it a small tweak… timed it, not really that much of a difference, but whatever… Suppose it helps to work it out either way.

you will probably get the most time gain if you go to see the already layed out math algorithms and convert them to code. this requires a bit more math knowledge than others to really solve with a decently fast algorithm

1 Like