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
 
xutopia,

in the original example 10 11 would be correct because of
if (++self.x >= 10) return self.x;


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

Finally the original example out!

Using venkman with the break points I realized that when foo() calls bar() which then in turn calls bar() again you get undefined because the interpreter builds this statement : alert(alert(self.x)) which will give you undefined because alert() returns void.

It's the same as writing out document.write(document.write(10))

The second time it doesn't do alert(alert(self.x)) because ++self.x >= 10 and bar() is not called again.

Does that make sense?

Gary Haran
********************************
 
sorry to ask this but what's this function supposed to do or show?

From what I remember a recursive function works in the principle of sending new values to the same function until a condition is met.
The X! example is the most used and common and it works under the concept of facto(X)=X*facto(X-1)
as in

function facto(num) {
if(num>1) return num*facto(--num);
return 1;

}

alert(facto(6));


now in your code I dont see a value being send back to the same function even though you are using a --self.x I don't think that guarantees you are working with the rigth values. I may be wrong I don't have a debugger like Vekman to check all the code but that's just old lessons from college coming to mind.
What's the purpose of the function?

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
 
xutopia,

nice - thanks for your effort! i should learn to use venkman.




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

"From what I remember a recursive function works in the principle of sending new values to the same function until a condition is met."

You are right. Here we didn't have recursion per say but rather a function calling itself.

Gary Haran
********************************
 
odd recursive function behavior
thread216-578200

grtfercho çB^]\..
"Imagination is more important than Knowledge" A. Einstein
 
hi jemminger

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

this is what i gave but try this:
j=0
function bar()
{
self.x=self.x?self.x:0
if (++self.x < 10)
{
bar();
}
else
{
return self.x
}
j++
alert(j)
}


guess what happened? it first gives the value of self.x then alerts the value of j from 0 to 10...

now i need more explanation...


Known is handfull, Unknown is worldfull
 
If you guys are interested in using the venkman debugger it can stop the execution and tell you what the value of a variable is at each line of code .

1. Using NS7.x go to 2. click &quot;install 0.9.77 for the easy setup.
3. Restarts your browser.
4. Tools > Web Developement > Javascript Debugger
5. follow tutorial here :
If you have a few loops that are complicated like these just :

1. Open your page in your browser
2. In the debugger's left pane click Watches
3. Right click and select Add Watch Expression
4. type the name of the variable you want to keep an eye on.
5. on the right (where the code is) click on the left of the numbers to add a breakpoint.

The interpreter pauses at each breakpoint until you click continue so you can see what values your variables are. You can also click all lines in your code to see how the code runs line by line.

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

Part and Inventory Search

Sponsor

Back
Top