Tell us what’s happening:
I don’t understand why my code is not working. It may not be the most efficent and I’m sure could be written better but it’s the principle of why it’s not working that has got me confused and why I’m posting!
For the tests in the exercise, it works for the first 4 but not for the last test (23 and 18).
smallestCommons([1, 5]) should return 60.
smallestCommons([5, 1]) should return 60.
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
I’m wondering if it’s some kind of execution timeout that’s causing the code to fail?
Your code so far
function smallestCommons(arr) {
console.log('arr', arr);
arr.sort();
var small = arr[0]; //smallest of the two numbers
var big = arr[1]; //biggest of the two numbers
var check = big - 1; // this is the lowest any common multiple could be
var found = false;
while (!found) {
check++;
if (check % small === 0 && check % big === 0 ) { // if common multiple
found = checkIfMultipleBetween(small,big,check);
}
}
console.log(check);
if (found) return check;
}
function checkIfMultipleBetween(small,big,check) {
for (var between = small + 1; between < big; between++) {
if (check % between !== 0) { // if not divisible by a number between
if (small == 18) console.log('failed on ' + between);
return false;
}
}
return true;
}
smallestCommons([1,5]);
Your browser information:
Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36
.
Link to the challenge:
https://www.freecodecamp.org/challenges/smallest-common-multiple