Tell us what’s happening:
Why do I receive the following output? This code returns 360360 outside of the FCC site.
Thanks!
// running tests
smallestCommons([1, 13])
should return 360360.
// tests completed
// console output
Potential infinite loop detected on line 14. Tests may be failing because of this.
Your code so far
function range(a, b) {
/* return an array of the numbers between a and b, inclusive */
const low = a < b ? a : b;
const high = a > b ? a : b;
let nums = [];
for (let i = low; i <= high; i++) {
nums.push(i);
}
return nums;
}
function smallestCommons(arr) {
const divisors = range(arr[0], arr[1]);
for (let i = 2; ; i++) {
let multiples = divisors.map(n => n * i);
let common = true;
for (let j in multiples) {
for (let k in divisors) {
if (multiples[j] % divisors[k] !== 0) {
common = false;
break;
}
}
if (common) {
const lcm = multiples[j];
return lcm;
} else {
break;
}
}
}
}
smallestCommons([1,5]);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0
.
Challenge: Smallest Common Multiple
Link to the challenge: