# Smallest Common Multiple YET AGAIN

This has by far been the hardest challenge in the JavaScript curriculum so far. And I know that something similar has been asked countless times before. But I am wondering if someone wiser than me in the ways of Javascript might critique my code. In spite of complaints in the curriculum console that there are potential infinite loops in two of my functions, this code does in fact pass. I take the warnings to mean I am pushing the limits on how inefficient my solution is.

It has taken me a couple of weeks and many false starts to get this method to work. But clearly there must be much more efficient ways. I did look at the hints and even the first suggested solution there but I could not understand that solution at all, so I just bashed my way through.

Any comments would be greatly appreciated!

Whew, this one kicked my butt!

my solution
``````function smallestCommons(arr) {

// find LCM for two numbers
function lowestCommonMultiple(x, y) {
for (let i = x; i <= x * y; i += x) {    //generate list of multiples of x
for (let j = y; j <= x * y; j += y) {  // generate list of multiples of y
if (i === j) { // find the match
return i;
}
}
}
}
/*
In the for loops the stopping condition is just the multiple of the two numbers,
since that is the last resort case.
I couldn't see how else to define the end of the loop.
*/

// create array of all numbers from the lower to the higher of input array
function setRange(x, y) {
if (y < x) {      // this block corrects for reversed order in input array
const newX = y;
const newY = x;
x = newX;
y = newY;
}
let array = [];
for (let i = x; i <= y; i++) {
array.push(i);
}
return array;
}

// run through array finding LCM of successive pairs
function arrayLowestCommon(array) {
let common = array[0];
for (let i = 0; i < array.length - 1; i++) {
common = lowestCommonMultiple(common, array[i + 1]);
}
return common;
}

const first = arr[0];
const last = arr[1];
const range = setRange(first, last);
const result = arrayLowestCommon(range);
return result;
}

``````