# Smallest Common Multiple Error

Smallest Common Multiple Error
0

#1

Tell us what’s happening:

I’m having an issue with the smallest common multiple challenge. The code works, but is failing one of the tests ( `smallestCommons([23, 18])` should return 6056820.) All the other tests pass, and when I plug it into the console with those arguments, it returns the correct number. My first thought is that it might be too slow (I’m sure it’s not the best possible solution) but it returns pretty much instantly in the console.

Your code so far

``````
function smallestCommons(arr) {
let arrHigh;
let arrLow;
let newArr = arr;
if (arr[0] > arr[1]) {
arrHigh = arr[0];
arrLow = arr[1];
}
else {
arrHigh = arr[1];
arrLow = arr[0];
}
let count = arrLow;
while (count < arrHigh) {
count++;
if (count < arrHigh) {newArr.push(count);}
}

let test = false;
let multiple = 0;

while (test == false) {
multiple = multiple + arrHigh;
for (let j = 0; j < arr.length; j++) {

if (multiple % newArr[j] == 0) {
test = true;
}
else {test = false;
break;}
}

}
return multiple;
}

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

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

#2

Hey @Nicholasch6,
Your code needs to be efficient to pass the challenge.
Your code might be turning the FCC’s infinite loop protection.

So, try to make your code a little efficient.

All the best.

#4

Figured it out, yea I guess it was loop protection. Limited the number of loops by iterating by multiples of the highest and lowest number, instead of just multiples of the highest number :
function smallestCommons(arr) {
let arrHigh;
let arrLow;
let newArr = arr;
if (arr[0] > arr[1]) {
arrHigh = arr[0];
arrLow = arr[1];
}
else {
arrHigh = arr[1];
arrLow = arr[0];
}
let count = arrLow;
while (count < arrHigh) {
count++;
if (count < arrHigh) {newArr.push(count);}
}

let test = false;
let multiple = 0;
let loop = 1;

while (test == false) {
multiple = arrHigh * arrLow * loop;
loop++;
for (let j = 0; j < arr.length; j++) {

``````  if (multiple % newArr[j] == 0) {
test = true;
}
else {test = false;
break;}
}
``````

}
return multiple;
}

smallestCommons([23,18]);

Still probably not the best but it works, so now I can look at the spoilers.

#5

And just realized I could make it even quicker by not bothering to test if arr[0] or arr[1] pass.