Tell us what’s happening:
I’m having an issue with the smallest common multiple challenge. The code works, but is failing one of the tests ( smallestCommons([23, 18])
should return 6056820.) All the other tests pass, and when I plug it into the console with those arguments, it returns the correct number. My first thought is that it might be too slow (I’m sure it’s not the best possible solution) but it returns pretty much instantly in the console.
Your code so far
function smallestCommons(arr) {
let arrHigh;
let arrLow;
let newArr = arr;
if (arr[0] > arr[1]) {
arrHigh = arr[0];
arrLow = arr[1];
}
else {
arrHigh = arr[1];
arrLow = arr[0];
}
let count = arrLow;
while (count < arrHigh) {
count++;
if (count < arrHigh) {newArr.push(count);}
}
let test = false;
let multiple = 0;
while (test == false) {
multiple = multiple + arrHigh;
for (let j = 0; j < arr.length; j++) {
if (multiple % newArr[j] == 0) {
test = true;
}
else {test = false;
break;}
}
}
return multiple;
}
smallestCommons([23,18]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/smallest-common-multiple