Here’s my solution:
function smallestCommons(arr) {
let range = [];
for (let i = Math.min(...arr); i < Math.max(...arr)+1; i++) {
range.push(i);
};
let j = range[range.length-1];
function lcd(value) {
return j % value === 0;
}
do {
j++
} while (range.every(lcd) !== true)
return j
}
Now I was super proud of myself when my code was returning the answers I was looking for but FCC doesn’t like this code. Why? I have a feeling it has something to do with the loop running too long or something like that.
My JS engine returns all of the correct answers for each test case except for the last one:
smallestCommons([23, 18]) should return 6056820.
When I attempt this one I get:
Possible infinite loop detected.
Error: Infinite loop
at smallestCommons (script 15:68)
at script 22:1
1905997
I’m not real sure how to proceed here. What is wrong?
you are very…very close. Hint: Right now, your function prints out what the result is to console.
Double check that test case that wont pass…especially the word in blue. 
1 Like
good eye, @cndragn. Its the little things, every time.
This, hands down, was the biggest gotcha when I first started learning. True story, many convos like this between me and my TA:
Me: Its not working. It says to return the value of blah blah, I did that and nothing is happening.
TA: It says to do what?
Me: It says to return the value of blah blah.
TA: It says to do what?
Me…omg.
…another gem
Me: I need help. It says to ret…omg.
TA: cracking up as I walk away
1 Like
@cndragn
Well this gotcha has certainly got me alright. I’ve tried swapping out the console.log() bit for return but still no dice. The blue word in the last test case… return?
Hrmm…well, we’re closer at least…yes, key word was return. In order for your function to return any value and be useful in your code, you have to use return, console log just prints out what that value is the console.
So you changed the line to return j;
and getting the same error?
Yes.
And let me clarify my code works for
smallestCommons([1, 13]) should return 360360.
in the JS engine I am using but it isn’t working in FCC for whatever reason.
So I am actually failing two cases:
smallestCommons([1, 13]) should return 360360.
smallestCommons([23, 18]) should return 6056820.
Just tried it…was baffled…and it hit me, that I know what it is, Ive come across this before but for the life of me not sure what the solution is.
Along the lines of, due to the structure of your code, when you run it one time, it works fine, but if you run one after another, as the test does, it affects the value. Okay, I understand Im not making much sense to you…if someone else knows what Im talking about please chime in. Setting off to figure this out though, give me a moment.
your solution, while pretty elegant, is kind of a brute force. You start from the highest number in your array, and check every digit after. way too inefficient. its timing out. wiki lcd algorithm.
Even if its not elegant, it should still pass…FCC doesnt really count that against us. The issue is not with the brute force method, its something specific in the code that is causing issues with the value as it continues to run tests in succession.
It isn’t the brute force, its how long brute force takes.
Ah ha! Found it! Its an infinite loop protection, and while searching around, it looks like this challenge in particular is susceptible to it. Its due to some limitations in the test environment.
After I found that, I searched on FCC for this challenge, and found a lot of threads of people having this exact same problem with this one in particular. I didnt look into them all in detail, but do a search on here for the challenge, there may be some tips and info to help you restructure your code.
I get what you mean…I was having a hard time remembering what the issue was, only that Id seen it before. Just was pointing out that being focused on writing the most elegant solution is not what FCC tests for…in most cases brute force, as a lot of early learners tend to do, is okay. With this particular challenge, not so much.
Will also admit, Im not keen on just giving the code to solve a problem. Especially as sbfergus was really proud to have been able to solve it (and should be!) I was kind of focused on working with the solution he came up with instead of the suggestion to just use another code instead.
But yeah, its a FCC thing, apparently with this specific challenge.
I also do code katas, daily coding challenges on a variety of areas. Many if not most are designed to require a solution pass in a shorter time. Half the challenge becomes efficiency. But yes, this is a solid elegant solution.
1 Like
I do appreciate the link and I will dig in in the future and report back. I’m a bit burnt out at the moment.
1 Like
Yeah, its definitely important to strive for the most efficient and elegant solution… I go back over my stuff all the time to try and improve on it. Just…remember it was like to just start out learning this stuff though. Managing to get any solution to work is an achievement.
Afterwards, once theres a much better grasp on the fundamentals, then its good, integral even, to move on from just solving a problem, to solving the problem in a better way. But learning what a loop is, while also learning time and space complexity is kind of a bit much…step by step.
1 Like
I feel ya…believe me, Ive been there many times where I was super frustrated because my code wasnt working, only to find out hours later my logic was correct, it was just that darned return statement or something equally simple.
Take a breather, but also take heart that you did solve the problem 
1 Like
Am I using //noprotect right in this case?
function smallestCommons(arr) {
//noprotect
let range = [];
for (let i = Math.min(...arr); i < Math.max(...arr)+1; i++) {
range.push(i);
};
let j = range[range.length-1];
function lcd(value) {
return j % value === 0;
}
do {
j++
} while (range.every(lcd) !== true)
return j
}
An update to my progress on this challenge:
I ended up just skipping it, finishing the rest of the JS course and projects then came back to it.
The approach I originally used was too burdensome for the computer. I couldn’t figure out a way of salvaging the existing code I wrote and ended up writing a new code all together, using a different mathematical approach to solve the problem.
2 Likes