If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Enjoy an ad free experience by logging in. Not a member yet? Register.

Unknown problem with my code? (Beginner)

Hey, I'm new to this forum and just started Javascript because my maths teacher challenged me to create a program to solve factoring questions. My code stops working at around the 20th line, and I don't know why. I've been using jsfiddle.net as my code compiler if that makes a difference.
This is my code:

I use the alert() thing to let me know when the code has passed a certain point so I can pinpoint where the problem is. Unfortunately, I know where the problem is, I just don't know what it is.
Thanks for taking the time to read this and, if you decide to help out, thanks for helping out!
-Tim

Sorry if this is in the wrong subforum, I didn't really understand what the different subforums were for, so I picked what I think is the most general one.

For those who are not smarter than 5th graders (lol): Tim's code is designed to identify the greatest common factor of three numbers.

The code is not sufficiently defensive. Sure, it tests for a zero denominator, which will crash the script, but imagine what might happen if someone entered 7 and 22... the recursive function would continue infinitely and cause a stack overflow.

I wonder if that is what might be happening in your case..?

Even if fast, recursion is memory-expensive, and the programmer MUST build into it a mechanism that ensures termination to avoid crashing the script.

I haven't combed through your code (too late at night for it) and recommend that you break it down for testing: try running gcf(a, b) to see if it crashes, and if not then try gcf(gcf(a, b), c). Do the same with your squaring loop. It's often easier to discover the problem when the code is simpler.

Also, may I suggest that your prompts can be more specific? The third prompt does not even imply that it is requesting a number(if you consider x and x^2 implications). While on the subject, you "might" want to insist that the user enters a natural number (i.e. > 0).

The code is not sufficiently defensive. Sure, it tests for a zero denominator, which will crash the script, but imagine what might happen if someone entered 7 and 22... the recursive function would continue infinitely and cause a stack overflow.

When I try entering 7, 22, and 3 in it returns 0 as the GCF. It's a translation of a euclidean algorithm, so there shouldn't be an issue.

[QUOTE=sbhmf;1392306]I haven't combed through your code (too late at night for it) and recommend that you break it down for testing: try running gcf(a, b) to see if it crashes, and if not then try gcf(gcf(a, b), c). Do the same with your squaring loop. It's often easier to discover the problem when the code is simpler.[\QUOTE]

Alright, thanks I'll try doing that.

Originally Posted by sbhmf

Also, may I suggest that your prompts can be more specific? The third prompt does not even imply that it is requesting a number(if you consider x and x^2 implications). While on the subject, you "might" want to insist that the user enters a natural number (i.e. > 0).

Right now it's really just designed for me. I'll make it more user-friendly once it actually works, until then there is no point in restating something that I already know if it's just for me.

Because there comes a point in some recursive solutions where you experience a stack overflow and your script blows up or it could take an agonizingly long time versus a simple iterative approach.
If you want to go recursive you may want to take a more dynamic programming approach and optmize via memoization.