Talk:Recursive Anonymous JavaScript Functions

From Jimbojw.com

Jump to: navigation, search

Comments on Recursive Anonymous JavaScript Functions

Note: Due to a recent influx of spam messages on this page, comments are now being moderated. Your comment will appear once it has been approved.
Leave a comment
Sorry, comments are disabled.

Contents

Drew said ...

Could you give an example of how to use this anonymous function in some example? I was unable to make this work in any way.

--Drew 08:18, 30 November 2007 (MST)

Jimbojw said ...

Sure thing Drew!

This is a contrived example, but here's a way to have a function which returns an anonymous recursive function:

function makeFactorial() {
  return (function(x){
    if (x<2) return x;
    return x * arguments.callee(x - 1);
  });
}

This could be called like such:

var factorial = makeFactorial();
factorial(3); // prints 6

The function "factorial" in the above example is recursive, but doesn't know its own name - which would be a problem without arguments.callee.

Hope this helps!

--Jim R. Wilson 11:20, 30 November 2007 (MST)

Daniel Skinner said ...

Useful tip. I never knew about arguments::callee before.

Whilst I don't have any immediate use for recursive anonymous functions I will definately be using arguments::callee in all recursive functions from now.

Is JS v1.2 available in most modern browsers?

--Daniel Skinner 14:07, 30 November 2007 (MST)

Jimbojw said ...

Hi Daniel,

> Is JS v1.2 available in most modern browsers?

Oh yeah. IE 6 claims support for JS 1.3, all other major browsers have something higher than that.

--Jimbojw 11:12, 3 December 2007 (MST)

Matt said ...

I found out recently that when running a recursive JavaScript function in Firefox there is a recursion limit built in (which can be handy to avoid the infinite loop mistakes, but really is a bummer when you truly need to recurse a function more than 1000 times). Not sure the exact limit but it appears to be between 800 and 1001. I haven't done much research, but I'm hoping there is a way to increase this limit for those who might knowingly need to recurse more than that (but then again, maybe they should be using something other than JavaScript for doing such a calculation?). Just my two cents on the topic.

--Matt 10:06, 7 December 2007 (MST)

Jimbojw said ...

Hi Matt,

Sometimes, even when you're sure that recursion is the way to solve a problem, there are other non-recursive solutions as well.

I recently wrote a followup to this article called Unrolling recursive JavaScript functions, wherein I describe the steps to unroll one particularly common type of JS recursion: DOM tree traversal.

Hope this helps, and good luck!

--Jimbojw 21:26, 7 December 2007 (MST)

Matt said ...

I'm just curious on what you think the best way to solve this would be...

Suppose you have an initial deposit of $1000.00 into a 4.78% interest (compounded daily) savings account. Say you also deposit a fixed payment into the same account at the end of each month. How much money would there be in the account at the end of the nth month (at which point you've made n payments)?

The compounded daily is where my recursion gets goofed when I try to do over 2 years... granted the interest earned with the same account compounded monthly isn't much difference, but I just wanted to see what it'd be compounded daily. Anyhow, there probably is a non recursive solution, but I don't have the time right now to figure it out... I'll post back here if I find time to come up with the non recursive solution. Thanks.

--Matt 15:36, 18 December 2007 (MST)

Jimbojw said ...

Hi Matt,

What you're trying to do, I think, is calculate the Future Value for an Increasing Annuity, which is defined thus:

an increasing annuity is an investment that is earning interest, and into which regular payments of a fixed amount are made.
-- from http://home.ubalt.edu/ntsbarsh/Business-stat/otherapplets/CompoundCal.htm

That website has good JavaScript based calculators, and explanations of all the formulae. Before you can use them though, you need to calculate the Effective Interest Rate of the APR.

So say you're compounding daily, and you want to know what the equivalent monthly interest would be. Let rM be the monthly interest as a proportion, rY be the APR as a proportion.

Then the formula is this: rM = (1 + rY/365)^30 - 1

Once you have that, then you can calculate the the future value of your increasing annuity.

For this problem, I would strongly advise against using any kind of recursion or looping since the mathematics behind these kinds of calculations do not require them.

Good luck!

--Jimbojw 10:18, 19 December 2007 (MST)

Matt said ...

I'm pretty sure you will miss out on a lot of interest over time (assuming my math is right) if you only compound it annually (using the APR) instead of daily... I already knew how to figure out the APR, but I did not want to do it that way as I want to compound the interest daily, meaning every months payment should start earning interest immediately instead of only at the yearly interval (and as previously mentioned the interest would be compounded daily)... I have created this site http://pixelspotlight.com/money.php which has my beginnings of a compound interest calculator, but it is using recursion right now... I have not spent much time on this, and the code is pretty ugly... a work in progress you might say. Of course my comment is assuming my math is right on my website, which it very well could not be... although I think it's at least very close to right. Any thoughts on how to do it without recursion?

--Matt 14:22, 19 December 2007 (MST)

Jimbojw said ...

> I'm pretty sure you will miss out on a lot of interest over time (assuming my math is right) if you only compound it annually (using the APR) instead of daily

Yes. That's why you have to calculate the effective interest rate over the period that makes the most sense. In this case, 1 month.

That is, you find out what one-time, end-of-month interest payment is equivalent to the yearly rate compounded daily over that month. I provided the formula to calculate this earlier.

You may have to multiply this value by 12 to get a new "adjusted" APR for use in the annuity calculation...

> Any thoughts on how to do it without recursion?

Yes. Please see the wikipedia page on Annuity (finance theory) for how to exactly calculate the future value of an increasing annuity.

--Jimbojw 16:02, 19 December 2007 (MST)