Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

recursion in javascript

Status
Not open for further replies.

werjocks

Programmer
Oct 27, 2003
24
0
0
US
Hi,
Im new to javascript... so bare with me.

I wrote a depth first negamax search engine in C for a tic-tac-toe program, which I've been working on translating to javascript for my webpage, and I am confident that the algorithms in my javascript match those in my working C program. What I've observed is, when a recursive call backs up from the bottom of the search tree, the values of the leaves have overwritten the values stored in the nodes above (as if it uses the same memory reference instead of storing old values to the stack for reuse later). I'll post the code when I get home, but for now my question is this... does javascript properly support recursion? Are there any documented limitations?

Also, if there is some kind of free javascript debugging tool... I'd appriciate anyone pointing me to that. using IE and alert statements to test is annoying.
 
I'm pretty sure that if you declare a function like this: var blah; it is a global variable shared throughout the whole program. Maybe try taking the var off the front of your variable declarations. I don't remember completely for sure if this is how it works, but it might give you somewhere to start.

-kaht

banghead.gif
 
>>I'm pretty sure that if you declare a function like this:

I meant variable, not function


-kaht

banghead.gif
 
i don't think that taking var off will make the variable local.

anuthing declared within a function is a local variable of that function. anything outside of any function in any place in your code - global variable, i.e 1 same shared between everything.

--------------------------------------------------
Goals are dreams with deadlines
-------------------------------------
 
It's 3:51AM and I've only just woken up. My coffee is still too hot to drink, so this post may be completely useless!!! :D

I've never needed recursion. Not even sure I know what it is, but reading the original post it sounds as though you want a history of a variables values?

var myVar = 'one&';

myVar = 'two&'+myVar; // update myVar
myVar = 'three&'+myVar; // update myVar

myVar.split('&')[0] // most recent value
myVar.split('&')[1] // previous value
myVar.split('&')[2] // and so on..

----------
I'm willing to trade custom scripts for... [see profile]
 
werjocks,

Just to let you know that recursion in Javascript is fine.

I'm not quite sure what the stack limit is, or if there are any other documented limits, but I've had no problems with many levels of recursion.

Dan
 
Billy..., how do you use it?

Can you post a really basic (one or two lines) demonstration?

----------
I'm willing to trade custom scripts for... [see profile]
 
stormbind,

a recursive function is one that calls itself:
Code:
function factorial(n) {
	self.x = self.x ? self.x : 1;

	if (n > 1) {
		self.x = self.x * n--;
		factorial(n);
	}
	return self.x;
}

alert( factorial(5) );

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top