# Discussion of decisions (...as an example - Smallest Common Multiple)

Hello!

Often in the tasks I see how the forum participants in the comments add their solutions. I think it’s a good way to find out about possible mistakes in your decision, but I’ve never been able to figure out how to do it.
Is this available after a certain level on the forum?
I give this decision as an example, because it seems to me too simple and I am afraid that in a real situation it can have negative consequences.

Spoiler
``````function smallestCommons(arr) {

let arrAll = [];
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
arrAll.push(i);
}

let sCM = 0;
for (let i = 1; !arrAll.every(num => i % num == 0); i++) {
sCM = i + 1;
}

return sCM;
}

console.log(smallestCommons([1, 13]));
``````

Challenge: Intermediate Algorithm Scripting - Smallest Common Multiple

Thanks, I’ll remember for the future.
Is there a problem with using .every in the second cycle?

Creating an array to use as loop bounds is mildly wasteful. Unfortunately, JavaScript doesn’t have a proper range iterator so you have to use this mildly wasteful approach if you want to get something like that.

Your for loop is not really a good fit - you don’t use i inside of the loop, so you shouldn’t declare i. You could use a while loop.

0 will never be the smallest common multiple. I’d start with a number that has a chance of being the smallest common multiple. Like a number in your range. Maybe the biggest one.

And, I’d check multiples of that starting number instead of adding 1 each time. That way you do fewer loops.

Perhaps useful to know (as you’re handling a range of consecutive numbers) that the SCM of any two consecutive numbers is their product (e.g. SCM of 7 and 8 is 56).

Thank you, if I got it right, then?

Spoiler
``````function smallestCommons(arr) {

let start = Math.min(...arr);
let stop = Math.max(...arr);
let arrAll = [];

for (let i = start; i <= stop; i++) {
arrAll.push(i);
}

let sCM;
for (let i = stop; !arrAll.every(num => i % num == 0); i += stop) {
sCM = i + stop;
}

return sCM;
}

console.log(smallestCommons([1, 13]));
``````

It works much faster.

I would still not use a for loop.

If you want the absolute fastest approach, look at the LCM + GCD solution.

Yep, while loop gives another 16% higher speed (approximately 2000 op/s).

The main part of the issue (regarding the discussion in the guide) remained unresolved. Is it worth creating a new topic every time there are doubts about the correctness of the solution?

Yes. The guide posts became a dumping ground for solutions instead of a place to discuss code, so we locked the posts.

It’s good to measure, though be careful chasing microbenchmarks. In this case, while is better than for because you are not using the for loop iteration variable at all, and generally you shouldn’t write code you don’t need.

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