I recently started teaching myself programming, so please excuse me if my question seems a bit basic.
One of the challenges on CodeCamp requires defining a function that takes an array with 2 values as input and returns the Least Common Multiple (LCM) of all numbers within that range inclusive of those 2 numbers.
My code passes the tests for numbers 1,2,3,6 in the exercise but fails for numbers 4 & 5. Surprisingly, I am not getting any error messages from freeCodeCamp! Therefore, I'm having trouble figuring out what's wrong with my code below.
function smallestCommons(arr) {
let allNum = [];
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
allNum.push(i);
}
function findFactors(x) {
let allFactors = [];
for (let i = 1; i <= x; i++) {
if (x % i == 0) {
allFactors.push(i);
}
}
return allFactors;
}
function findGCF(a,b) {
return findFactors(a).filter(item => findFactors(b).includes(item)).reduce((p,q) => p*q);
}
return allNum.reduce((a,b) => ((a*b)/findGCF(a,b)));
}
The test cases provided are as follows. My code passes 1,2,3 & 6 but fails 4 & 5.
smallestCommons([1, 5]) should return a number.
smallestCommons([1, 5]) should return 60.
smallestCommons([5, 1]) should return 60.
smallestCommons([2, 10]) should return 2520.
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
function smallestCommons(arr) {
let allNum = [];
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
allNum.push(i);
}
function findFactors(x) {
let allFactors = [];
for (let i = 1; i <= x; i++) {
if (x % i == 0) {
allFactors.push(i);
}
}
return allFactors;
}
function findGCF(a,b) {
return findFactors(a).filter(item => findFactors(b).includes(item)).reduce((p,q) => p*q);
}
return allNum.reduce((a,b) => ((a*b)/findGCF(a,b)));
}
console.log(smallestCommons([1, 5])); // should return a number.
console.log(smallestCommons([1, 5])); // should return 60.
console.log(smallestCommons([5, 1])); // should return 60.
console.log(smallestCommons([2, 10])); // should return 2520.
console.log(smallestCommons([1, 13])); // should return 360360.
console.log(smallestCommons([23, 18])); // should return 6056820.