Can someone explain to me the logic of this algorithm (Smallest Common Multiple)?

Tell us what’s happening:

Hi guys. I wrote the first block of this code, but the rest comes from the solution, which I shamefully had to look at. It works, and I understand what is going on for the most part, except for one thing:

In line 14, where I wrote console.log(GCD), I get this list of numbers: 1, 2, 1, 6, 5, 4, 3, 2. The 1, 2, 1 is what I would expect from the formula in findGCD, but can someone explain where the 6, 5, 4, 3, 2 come from? Numbers I cannot wrap my head around appear on the console, no matter which parameter I use from the problem. I’m just not following the logic. Thanks.

Your code so far

function smallestCommons(arr) {
 arr.sort(function(a, b ) {
   return b-a;
 var sorted = [];
for (var i = arr[0]; i >= arr[1]; i--) {

var lcm = sorted[0];
for (var i = 1; i < sorted.length; i++) {
var GCD = findGCD(lcm, sorted[i]);
var lcm = (lcm * sorted[i]) / GCD;
return lcm;

function findGCD(x, y) {
   if (y === 0) {
     return x;
   else {
     return findGCD(y, x % y);

smallestCommons([2, 10]);

Your browser information:

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

Challenge: Smallest Common Multiple

Link to the challenge:

Hello @anthonymantilla555,

In this case, first I suggest you to use step by step code execution visualizer - (choose JS in the drop-down).

If you still have questions after, feel free to ask. :slight_smile:

Wow. I didn’t know this was a thing. It helped. Thanks!