Browser crashes when running code at "Smallest Common Multiple" task

Hi, everyone!

I need some help. I ran my code with these parameters [1, 5], [5,1], and I even tested it with [1,12], and it worked. Yet when I try [1,13], my browser crashes. The same happens when I click “Run tests” button.

Could you give me any suggestions of what I’m doing wrong? Thanks in advance for your help.

Here is my code.

Most browser crashes are due to infinite or long running loops. Try adding some console.log(‘location 1’) type tests and running it again in your browser with the developers console open. Your colsole should log where the long loop is happening…

I noticed that browser window recovered and I see green flags opposite to all the function tests. Does it mean that I’ve made this assignment? I’m staying on the same page and I haven’t been redirected to the next challenge page.

When you hit “Run Tests” and you get all green check marks next to the tests your code should have passed. You may not be getting to the next test because something odd happen with that long running test, so try reloading the page, paste the code back in and try running it again. Also I got caught a few times when for some reason I was no longer logged in, so try logging out and back in again. If still no luck try clearing your browsers cache, that has helped some campers. Last try a different browser.

Remember to save a copy of your code first!!

Eventually I passed the test and got to the next stage. Still I’m concerned of the amount of time my script took to execute. There might be something isn’t good in the script’s algorithm. Unfortunately, my programming skill isn’t good enough to figure out what it is.

:point_up:
I’ve had so start over on one of the tasks.
I’m pretty sure I had everything correct after fixing a few mistakes but the tests would keep failing.
Maybe try this just to see if it works. The tasks are normally quickly repeated.
Dave

Thing I noticed: you are returning counter, but you are only increment counter by one every time the loop runs.
So the loop must run 27720 times for [1, 12], but 360360 times for [1, 13].
Your increments should be at least the size of the biggest number (e.g. 13, 26, 39 etc.).

Simplest algorithm is to take the biggest number (13), check if it is divided even by all the numbers in range (1…13, but you can omit 1 and at first non-even division you stop the check), if not, add biggest number to itself (13 + 13), repeat the check etc.

Thanks for your advice. It runs much faster now. I’ve realised by now why my code took too long to run. I developed my algorithm so that the function could take any numbers as parameters ( [1,5], [3,6], [1,13], [2,13], etc). Yet the lowest number of the parameters array is intended to be 1. I must have misunderstood the assignment.

Your algorithm looks to be “brute force”. Consider that LCM (Least Common Multiple) can be expressed with GCD (Greatest Common Denominator) with this function:

lcm(a,b) = a * b / gcd(a,b)

For GCD, you can use Euclid’s algorithm, pseudocode for which you can find on wikipedia.

1 Like

I had the same issue where I passed the first 3 and couldn’t pass the last one (Note I’m using the brute force method).
The oddest thing was the exit value was different every time and the loop I was using exited even though the while(condition) was not met. I tried adding //noprotect on line one and the last one passed. It appears that the infinite loop protection was forcing the exit.
If you are going to try this make sure you do not have an infinite loop.