curriculum/challenges/english/blocks/lab-range-based-lcm-calculator/ae9defd7acaf69703ab432ea.md
In this lab, you will create a function that takes an array of two numbers and returns the least common multiple (LCM) of those two numbers and all the numbers between them.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
User Stories
smallestCommons function that accepts an array of two numbers as an argument.smallestCommons function should return the smallest common multiple that is evenly divisible by both numbers and all sequential numbers in the range between them.You should have a smallestCommons function.
assert.isFunction(smallestCommons);
smallestCommons([1, 5]) should return a number.
assert.isNumber(smallestCommons([1, 5]));
smallestCommons([1, 5]) should return 60.
assert.strictEqual(smallestCommons([1, 5]), 60);
smallestCommons([5, 1]) should return 60.
assert.strictEqual(smallestCommons([5, 1]), 60);
smallestCommons([2, 10]) should return 2520.
assert.strictEqual(smallestCommons([2, 10]), 2520);
smallestCommons([1, 13]) should return 360360.
assert.strictEqual(smallestCommons([1, 13]), 360360);
smallestCommons([23, 18]) should return 6056820.
assert.strictEqual(smallestCommons([23, 18]), 6056820);
function smallestCommons(arr) {
let [min, max] = arr.sort((a, b) => a - b);
function gcd(a, b) {
return b === 0 ? a : gcd(b, a % b);
}
function lcm(a, b) {
return (a * b) / gcd(a, b);
}
let multiple = min;
for (let i = min + 1; i <= max; i++) {
multiple = lcm(multiple, i);
}
return multiple;
}