Tell us what’s happening:
Hello all. I have figured out a solution to this problem but with limitation. My while
loop needs to be set to a high number IOT process larger inputs. I currently have it set to 10000 to pass the challenge but I’m sure there’s a way to make it stop once the condition is met .
I have also come up with this…
while (commonCounter !== sortedArr.forEach(x => {
if(commonCounter % x !== 0) {
commonCounter = commonCounter + commonMultiple;
}}));
…and although it works as expected, I get a potential loop error and then after about 10 seconds it passes the challenge.
Any suggestions on a better way to implement a loop here?
Thanks in advance
**Your code so far**
[spoiler]
function smallestCommons(arr) {
//sorting numerically
let sortedArr = [];
let sorted = arr.sort((a,b) => a - b);
//loop used for pushing numbers between sorted[0] and sorted[1]
for (let i=sorted[0];i<=sorted[1];i++) {
sortedArr.push(i);
}
console.log(sortedArr)
//create a common multiple by using sorted[0] times sorted[1]
let commonMultiple = sorted[0] * sorted[1];
let commonCounter = commonMultiple;
let r = 0;
while (r < 10000) {
sortedArr.forEach(x => {
if(commonCounter % x !== 0) {
//continue adding commonMultiple until all elements (x) evenly divide
commonCounter = commonCounter + commonMultiple;
}});
r++
};
console.log(commonCounter)
return commonCounter;
}
smallestCommons([13,1]);
[/spoiler]
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36 Edg/88.0.705.63
.
Challenge: Smallest Common Multiple
Link to the challenge: