Smallest Common Multiple of all numbers in a range, explain n = 2?

Can someone explain how this code works correctly?

So I don’t understand how the guide comes up with ‘n=2’ to iterate through the index of our array. for example an array [5,4,3,2,1] will never see 5 or 4. Furthermore, I don’t see how LCM is reached if the divisor is never 4 (from our example). I printed n to test and it is never 1, 4, or 5.

function smallestCommons(arr) {
arr.sort( (a,b) => {return b-a});
let newArr = [];
for(let i = arr[0]; i>=arr[1]; i–){
newArr.push(i);
}

let quot = 0;
let loop = 1;
let n;

do{
quot = newArr[0] * loop * newArr[1];
for(n=2; n < newArr.length; n++){
console.log('n= ’ + newArr[n])
if(quot % newArr[n] !== 0){
break;
}
}
loop++;
}while(n !== newArr.length);
return quot;

}

Not sure if this will help you…
https://www.mathsisfun.com/least-common-multiple.html

The code doesn’t work correctly, at least if you’re trying to get the least common multiple.

The first for-loop simply gets the two largest numbers from the sorted array, which can more simply be achieved through arr.sort(/* sort code */).slice(0, 2)

The second for-loop is never reached, since n = 2, and therefore is not less than newArr.length (= 2).

The do-while loop breaks immediately after multiplying 5 and 4 and (redundantly) increasing loop, because n === newArr.length(again, 2).

Therefore, this is just a lot of code to multiply the two largest numbers in your array. The entire function accomplishes the same thing as:

function multiplyTwoLargest (arr) {

let newArr = arr.sort((a, b) => b - a).slice(0, 2)
return newArr[0] * newArr[1]

}

It might be helpful if you include the link to the guide that you’re referring to.

I should have been more clear, we are looking for LCM for the entire range between and including 2 given numbers. The code is from their solutions guide and does give the correct result but I dont understand how if we’re missing part of the range. Here’s the description:

Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.

The range will be an array of two numbers that will not necessarily be in numerical order.

For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.