Hey guys,
I’m working on completing the smallest commons algorithm challenge. My code seems to have the correct output locally (through Atom), but fails the two following tests on FCC:
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
Here’s my code:
function smallestCommons(arr) {
arr.sort( (a, b) => { // puts values in order
return a - b;
});
let newArray = []; // new array of each integer in between
for (let i = arr[0]; i <= arr[1]; i++) {
newArray.push(i);
}
let max = newArray.reduce( (acc, value) => { // the largest possible lcm
return acc * value;
});
for (let i = newArray[newArray.length - 1]; i <= max; i++) { // iterates through each possible lcm
let divCheck = newArray.every( (value) => { // returns true if number is divisble by every value in the array
return i % value == 0;
});
if (divCheck == true) { // returns the truthy value
return i;
}
}
}
Any suggestions or advice on how to pass the test would be great and, more importantly, any tips on how to write better code. Thanks!