Variable becomes undefined in recursive javascript function?

RachelH

New Member
I've got a simple script that I've written to find greatest common divisors (GCDs) and least common multiples (LCMs) using a recursive method- Euclid's algorithm.Unfortunately if it needs to do this in more than one step the relevant variable to be returned from the function becomes undefined. I've tried following it in a debugger with breakpoints, which seems to follow the recursion adequately and go back to the original function appropriately, but it then mysteriously vanishes at the end of the function even though it's meant to be returned?Not sure why this is happening or what I can do to fix it. My code is below:\[code\]function GCD(a, b) { if (a % b == 0) { return b; } else { GCD(b, (a % b)); }}function LCM (a, b) { return (a*b)/GCD(a, b);}function makeDM (a, b) { return (GCD(a, b) + " " + LCM(a, b));}\[/code\]So if you use a & b such as 60, 20 it will give the correct answers of 20 and 60. However if you use numbers such as 20, 60 or 126, 35 it fails miserably.
 
Back
Top