# Smallest Common Multiple - inefficient code or mistakes?

Tell us what’s happening:
After last challenge i suspect that my code not efficient for FCC again - i got 4 of 6 passes and it looks like because of slow process of very long iteration i don’t get passes for big numbers. Am i right or it is just mistakes in code - not his inneficiency? And how to understand that your code slow and inefficient?

``````
function smallestCommons(arr) {
arr = arr.sort((a,b) => {return a - b});
let nar = [];
for (var i = arr; i <= arr; i++) {
nar.push(i);
}
let check = 0;
let result;
var j = nar*nar[nar.length-1];
for (j; check !== nar.length; j++) {
check = 0;
for (var k = 0; k < nar.length; k++) {
if (Number.isInteger(j/nar[k])) {
check++;
}
if (check === nar.length) {
result = j;
}
}

}

return result;
}

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

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

I believe that your algorithm does produce the correct result but takes too long. You are incrementing your SCM candidate by one even though that results in testing lots of numbers that could not possibly be the SCM

I improved it a little bit =) 5 of 6

``````function smallestCommons(arr) {
arr = arr.sort((a,b) => {return a - b});
let nar = [];
for (var i = arr; i <= arr; i++) {
nar.push(i);
}
let check = 0;
let result;

for (var j = nar[nar.length-1]; check !== nar.length; j+=nar[nar.length-1]) {
check = 0;
for (var k = 0; k < nar.length; k++) {
if (j%nar[k] == 0) {
check++;
}
else {break;}
}
}
if (check === nar.length) {

result = j-nar[nar.length-1];
}

return result;
}

smallestCommons([5,1]);
``````

I did it. 6 of 6

``````function smallestCommons(arr) {
arr = arr.sort((a,b) => {return a - b});
let nar = [];
for (var i = arr; i <= arr; i++) {
nar.push(i);
}
let check = 0;

for (var j = nar[nar.length-1]*nar[nar.length-2]; check !== nar.length;
j+=(nar[nar.length-1]*nar[nar.length-2])) {
check = 0;
for (var k = 0; k < nar.length; k++) {
if (j%nar[k] == 0) {
check++;
}
else {break;}
}
}
return j-nar[nar.length-1]*nar[nar.length-2];
}

smallestCommons([1, 5]);

``````
1 Like