function checkRange(num, temp) {
for (var i = 1; i < num; i++) {
console.log(temp % i, i, temp);
if (temp % i != 0) {
return false;
}
}
return true;
}
function smallestCommons(arr) {
arr.sort((a, b) => {return a > b})
var two = [arr[1]];
var check = false;
while (check == false) {
two.push(two[two.length - 1] + arr[1])
if (checkRange(arr[1], two[two.length - 1]) == true) {
check = true;
return two[two.length - 1];
}
}
console.log(two);
// not sure what to do with this
return two[two.length - 1];
}
smallestCommons([1, 13]);
So I do realize that it's probably an infinite loop, but I would like to know why this is happening.
My code is not working for:
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
The code works with the following steps:
Get lower number as first index (sort)
Make a loop that will keep adding the last index with arr[1] and validate if every number counting up to arr[0] can be divided evenly for the last element of the array.
check
totrue
. Exiting the loop therefore requires(countDown(arr[1], two[two.length - 1]) == true
to evaluate totrue
.countDown()
could be named better. I'm pretty sure its purpose is not to count down.