I've been stuck in this free code camp algorithm and really need some help.
this is what i am supposed to do:
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.
e.g. for 1 and 3 - find the smallest common multiple of both 1 and 3 that is evenly divisible by all numbers between 1 and 3.
i have to check weather each value in my arr is divisible by my common multiple and really haven't been able to accomplish it.
this is what i have so far:
function smallestCommons(arr) {
arr = arr.sort();
var number = arr[0];
var secArr = [];
// create a new list with all values to check against.
while (number >= arr[0] && number <= arr[1]) {
secArr.push(number);
number++;
}
var commonMultiple = 1;
var isTrue = true;
function isDivisible(item) {
if (item % commonMultiple === 0) {
return true;
}
else {
return false;
}
}
while (isTrue) {
commonMultiple++;
if (secArr.every(isDivisible)) {
isTrue = false;
}
}
return commonMultiple;
}
smallestCommons([5,1]);
I tried to solve this problem, with Euclid's algorithm and thought it was very hard, tried with for loops and couldn't, I'm currently trying to check with .every
but it says i have an infinite loop.