An Issue with Smallest Common Multiple

An Issue with Smallest Common Multiple
0

#1

Hi!
I was struggling with this challenge for a while and finally came up with a solution.
That sure is not elegant, but I am a beginner.

function smallestCommon(arr){
	let newArr = [];
	let max = Math.max(...arr);
	let min = Math.min(...arr);
	for (let i = min; i <= max; i++){
		newArr.push(i);
	}
	let fin = newArr
	 .sort((a,b)=>(a<b))//I used .sort() in order to decrease the number of computations (you'll get the lcm faster when 
	 //dealing with larger numbers), though I still can't come up with an idea how to quit the function when the lcm is found.
	 .reduce((a,b)=>(a*b/euclidianR(a,b)));
	 return fin;
//This is a function for the Euclid's Algorithm.
	function euclidianR(a,b){
	while(b !== 0) {
		let remainder = a % b;
		a = b;
		b = remainder;
	}
	return a;
}
}

So my question is how How could I stop executing the .reduce() when I get the needed value(LCM)?
i.e.: In case you deal with [2,10],
It takes less computations if you start with 10 (you’re done when you reach 7), then if you start with 2.

And one more thing.
Prior to that solution, I was trying to use the prime factorization.

This is an extract from my previous code. Suppose, we only have to deal with the lcm variable

let arr = [];
let lcm = [2,3,4,5,6,7,8,9,10];
lcm.forEach(function(x) {
    for (let i = 2; i <= x; i++) {
        if(x % i === 0) {
            let count = 0;
            while(x % i === 0) {
                x = x / i;
                count++;
            }
            arr.push([i, count])
        }
    }
})

and if you return the arr variable, you’ll get this:

[ [ 2, 1 ], [ 3, 1 ], [ 2, 2 ], [ 5, 1 ], [ 2, 1 ], [ 3, 1 ],
[ 7, 1 ], [ 2, 3 ], [ 3, 2 ], [ 2, 1 ], [ 5, 1 ]]

As you might notice, the second element of each subarray is an exponent for the first element.
Now all I have to do is to flatten this array, so that all unnecessary factors are not included. For example, in this case we only need two “3”-elements.

How could this be done?
Thank you .