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!

odd recursive function behavior 3

Status
Not open for further replies.

jemminger

Programmer
Jun 25, 2001
3,453
0
0
US
try this out - can anyone explain why you get "undefined", "11"???

[tt]
function foo(o) {
alert(bar())
}

function bar() {
self.x = self.x ? self.x : 0;
if (++self.x >= 10) return self.x;
else bar();
}

foo();
foo();
[/tt]


=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
is it because foo(o) has an argument called o

but foo() doesnt?

or

u havent declared o (before the fns)

i am now only trying it these are the doubts i have as i see it...

Known is handfull, Unknown is worldfull
 
disregard argument "o" - you can take it out of foo.

you get the same result with or without it



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
I ran this and get "undefined", "11", "12".

I haven't figured out why you're getting "undefined", but x is set to 10 at that point, so the next time you call it, it gets set to 11, and the next time it gets set to 12.

function foo() {
alert(bar())
}

function bar() {
alert("self.x=" + self.x);
self.x = self.x ? self.x : 0;
//alert("self.x=" + self.x);
if (++self.x >= 10) return self.x;
else bar();
}

foo();
alert("foo 1");
foo();
alert("foo 2");
foo();
alert("foo 3");

Also, the foo() function has nothing to do with it. I get the exact same results with...

alert(bar());
alert(bar());
alert(bar());


Good Luck! [thumbsup2]
WindUp
 
buddy i am trying
even this is not working
if (++self.x >= 10) return "5"; (first time)


Known is handfull, Unknown is worldfull
 
I'm not sure if this was your intended behavior, but this returns "10", "11", "12". Note the addition of the last return statement.

The reason is that the 10th iteration returns "10", the next returns "9", the next "8"... as they pop off the stack; when it gets back the the very first iteration it goes to the next statement after "else bar();". In the original function there was no return statement after that, so the return value was "undefined".

function bar() {
self.x = self.x ? self.x : 0;
if (++self.x >= 10) return self.x;
else bar();
return self.x
}
alert(bar());
alert(bar());
alert(bar());


Good Luck! [thumbsup2]
WindUp
 
as windup said the return stmt must be outside the if stmt (god knows why)?
this worked fine for me
function foo()
{
x=0
alert(bar())
}

function bar()
{
x++
if (x < 10)
{
bar()
}
return x
}


Known is handfull, Unknown is worldfull
 
windup,

thanks for your input...

&quot;In the original function there was no return statement after that, so the return value was 'undefined'.&quot;

but isn't this how you construct a recursive function?

function recursive() {
if (conditionIsMet) return value;
else recursive();
}

there should only be a return value when the function is done, right?









=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
function bar()
{
x++
if (x < 10)
{
bar()
}
return x
}

vbkris, interesting...i'll see what i come up with using this format

=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
vbkris,

thanks for your help...the syntax seems funny, but i guess it's the same as vbs:

function foo()
for x = 1 to 10
foo = foo & x
next x
end function



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
windup, thanks for your input...
but isn't this how you construct a recursive function?


Close, it should be like this (note the return after the else)...

function recursive() {
if (conditionIsMet) return value;
else return recursive();
}


Good Luck! [thumbsup2]
WindUp
 
thanks windup

scriptohead,
actually, that's what this line does:
self.x = self.x ? self.x : 0;



=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
Yes, but sometimes there are problems if values are not explicitly initialised first. I thought it might be that.

~mgb
 
jemminger,

When you ask a question I'm real interested! It means we have a challenge here!

self is not a part of the function but a part of window. When you reference self.x in your function you actually reference window.self.x :

alert(window.self = self) // alerts true

This means that when you call your function a second time window.self.x is reinitialized. The way to get around this is to combine Windup's return and use a parameter like so :

function foo(param)
{
alert(bar(param))
}

function bar(param)
{
param= param|| 0
if (++param >= 10) return param;
else return bar(param);
}

foo();
foo();

Let me know if this is what you needed

PS : you could also use a one liner for the return return(++param >= 10) ? param: bar(param ;

Gary Haran
********************************
 
Jeff,
Your original script is fine, you're just missing one keyword:

<html>
<head>
<script>
function foo(o) {
alert(bar())
}

function bar() {
self.x = self.x ? self.x : 0;
if (++self.x >= 10) return self.x;
else return bar();
}

foo();
foo();
</script>

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
xutopia,

hmm...i realize that self is synonymous with window, and i did notice but couldn't figure out why self.x would be reinitialized to zero each time the function was called. what i was expecting to happen is this:

if self.x does not exist,
self.x = 0;
else
self.x++;

but in reality all i ever got was
self.x = 0;

i'm still not clear on why this is, since you can store global vars as window.varname or self.varname, right?

why does making the function recursive cause it to behave differently than this:
function a() {
self.x = self.x?self.x:0;
alert(self.x++);
}







=========================================================
try { succeed(); } catch(E) { tryAgain(); }
-jeff
 
xutopia,
what were you expecting?

Adam
while(ignorance==true){perpetuate(violence,fear,hatred);life--};
 
Adam,

I was expecting 10 10. Is this right Jeff?

Jeff,

I've downloaded NS7 and installed venkman to debug properly using breakpoints. This begs for such tools to get at full depth here. I'll test using venkman and if I find anything will post here.

Gary Haran
********************************
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top