# Intermediate Algorithm Scripting - Smallest Common Multiple

hello there, the more i progress in the course , the more my solutions are varying drastically from the offered solutions when i check them against them after completing a challange , and i sometimes have trouble understanding the solutions offered on the forum …

example my code for this challane seems a bit overconvuluted compared to the solution oferred.

my solution:

``````function smallestCommons(arr) {
if (arr[0] > arr[1]) {
let a = arr[0];
arr[0] = arr[1];
arr[1] = a;
}
let darr = []; // series of numbers to check for lcm
let num;   // instance of number from prime numbers array
let farr = [2];// prime numbers array  to be converted to let parr later

let omgarr = []; // final array to multiply for lcm
let b = arr[0];// iterator  to create  darr[]

for (let i = 0; i < arr[1] - arr[0] + 1; i++) {
darr.push(b);
b++;
}

for (let i = 3; i <= arr[1]; i++) {
if (i % 2 !== 0) {
farr.push(i);
}
}
for (let i = 1; i < farr.length; i++) {
let pushable = true;
for (let j = 1; j < farr.length; j++) {
if (farr[i] !== farr[j]) {
if (farr[i] % farr[j] == 0) {
pushable = false;
}
}
}
if (!pushable) {
farr[i] = 0;
}
}
let parr = farr.filter((elem) => elem !== 0);
console.log(parr);
console.log(darr);

for (let i = 0; i < parr.length; i++) {
let num = parr[i];

for (let j = 0; j < 20; j++) {
let copy = darr.join("-");
console.log(copy);

for (let k = 0; k < darr.length; k++) {
if (darr[k] % num === 0) {
darr[k] = darr[k] / num;

}
}
if (copy !== darr.join("-")) {
omgarr.push(num);
console.log("pushed" + omgarr);
}
}
}

console.log(darr);

console.log(omgarr);

return omgarr.reduce((acc, elem) => elem * acc, 1);
}

smallestCommons([23, 18]);

``````

should i be orried about the way i am solving the challanges or does the end justify the means?

It is good that you are solving the problem. But this is another one where I’d try to do it without making any arrays.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.