 # Smallest common multiple. Just another solution

Hi campers!
This challenge took me a lot of time, but at the end I can solve it, with probably the worst possible algorithm … but it works! I applied the fundamental theorem of arithmetics. What do you think about this approach?

Here is my code:

``````function smallestCommons(arr) {
var arr1 = [];
var arr2 = [];
var arr3 = [];
var temp =[];
var prim = [];
var primExp = [];
var lcm;

//fill the array between the two given values
for(var i=Math.max(arr, arr); i>=Math.min(arr, arr); i--){
arr1.push(i);
}

//create an array with all the prime numbers between arr.max and arr.min
for(var p=0; p<=Math.max(arr, arr)+1; p++){
temp.push(p);
for(var w=2; w<=Math.sqrt(p); w++) {
if(p!==w && p%w ===0){
delete temp[p];
delete temp;
prim = temp.filter(Boolean).reverse();
}
}
}

//apply the Fundamental theorem of arithmetic (prime factorization)
for(var j in arr1){
for(var k in prim) {
while(arr1[j]%prim[k] === 0){
arr1[j] = arr1[j]/prim[k];
arr2.push(prim[k]);
}
}
}

// Get the count of the longest repeating streak of each prime within arr2
function highestPow(value, character) {
var count = 0;
var maximum = 0;
var expected;
var index;

expected = index = value.indexOf(character);

while (index !== -1) {
count++;

if (index === expected) {
if (count > maximum) {
maximum = count;
}
} else {
count = 1;
}

expected = index + 1;
index = value.indexOf(character, expected);
}

return maximum;
}

//create an array with the highest power of each prime number
for(var hp=1; hp<=arr2; hp++){

a = Math.pow(hp, highestPow(arr2, hp));
arr3.push(a);

}
//multiply the highest power of each prime number together to obtain the lcm
lcm = arr3.join(',').split(',').reduce(function(a,b){return a*b;});
console.log(lcm);
return lcm;
}

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