Tell us what’s happening:
everything else works, but
smallestCommons[1, 13] and [23, 18] don’t work…
is my code an infinite loop???
Your code so far
function smallestCommons(arr) {
// creates a variable to store the smallest num
var start = Math.min(...arr);
// store the biggest num
var end = Math.max(...arr);
var sortedArray = [];
var LCM = 0, flag = true;
// adds from smallest to largest values into sortedArray
for (let i = start; i <= end; i++){
sortedArray.push(i);
}
while (flag){
LCM++;
// for all elements in sortedArray,
for(let n = 0; n < sortedArray.length; n++){
// if LCM / element gives a remainder which means that number is not divisible by LCM
if (LCM % sortedArray[n] !== 0){
// break out of the for loop
break;
// if above condition is met (if LCM / element results no remainder), checks if the current element is the last element,
}else if (sortedArray[n] === end){
// if current element is the last element in an array, exit the while loop
flag = false;
}
}
}
return LCM;
}
smallestCommons([1,5]);
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