Smallest Common Multiple please help!

Tell us what’s happening:
So the code works fine in the google chrome console but when I run the code in the freeCodeCamp console and run the test the last smallestCommons([23, 18]); test fails but on the google chrome console I get correct answer. Please can you guys help me what is wrong? I know the code isn’t the most efficient code so I am wondering if its due to efficiency issue?

Your code so far


function smallestCommons(arr) {

 let arrSort = arr.sort(function(a,b) {
	return a-b 
 });

let numBetween = [];

for (let i = arrSort[1]; i >= arrSort[0]; i--) {
	numBetween.push(i);
}

let multiple = false;
let biggestCommon = numBetween[0];
let count = 1
while (multiple === false) {
	let check = [];
	
	for (let i = 0; i < numBetween.length; i++) {
		if(biggestCommon % numBetween[i] === 0) {
			check.push(true);
        } 
    }
		if(check.length === numBetween.length) {
		multiple = true; 
    	} else {
		count++;
		biggestCommon = numBetween[0] * count;
		check = [];
		
    	} 
    }
	return biggestCommon;
}


smallestCommons([23, 18]);
smallestCommons([1, 13]);
smallestCommons([2, 10]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.132 Safari/537.36.

Link to the challenge:

It seems you are triggering the infinite loop protection. if your code takes too much to execute it will be stopped prematurely. you need to write a code that will execute faster.

1 Like