Question:
Find the smallest common multiple of the provided parameters that can be evenly divided by both, as well as by all sequential numbers in the range between these parameters.
The range will be an array of two numbers that will not necessarily be in numerical order.
For example, if given 1 and 3, find the smallest common multiple of both 1 and 3 that is also evenly divisible by all numbers between 1 and 3. The answer here would be 6.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Answer:
function smallestCommons(arr) {
let numbers = []
for (let i = Math.min(...arr); i <= Math.max(...arr); i++) {
numbers.push(i);
};
let n = numbers[numbers.length-1];
while (true) {
if (numbers.map((item => n%item)).reduce((a,b) => a+b) == 0) {
return n;
}
else {
n++;
};
};
};
smallestCommons([1,5]);
console.log(smallestCommons([23,18]));
I get all the expected answers, but I could not get that:
smallestCommons([23, 18]) should return 6056820.
Any thoughts?
Thanks in advance!