15 02 2010
“Everything old is new again”
Further thinking gave me the idea that it would be nice to have this function do something more weird and confusing. This useless function should give as the output the number of times it has been called (for example in alert box). The challenge of this puzzle is that it must give output only at the end of the execution, with the last call.
That means that code like that
fnc()()()()()()() MUST show one alert box with text “Fnc called 7 times”.
After some experiments I found a solution for this puzzle and I figured that you might also want to challenge yourself by finding out the way it can be done. So, I propose everyone to post their solutions in the comments.
Try it live :
If you decided that it’s enough for you to break your head with this puzzle, you can always check my solution (in html code 🙂 ).
I hope you liked my idea, and also get enthusiastic, like I did, when I was searching for the answer.
The original entry was posted on Saturday, July 12th, 2008 at 4:12 pm
Great quiz 🙂
My first try was to create a curried function, but then i realized that i would never know when the function was finished, so i came up with the following function, which uses some kind of threading, by setting a timeout, it delays an anonymous function until the current calls are completed. f.max is a ‘static’ variable which is available accross all invocation of f. i and its current value however are hitched in the scope of the anonymous function. So only for the last instantation/ call of this function the scope of the function wil hold the proper ‘i’, resulting in the proper alert message.
Good solution 🙂 I like this creative approach by checking i == f.max 🙂
In my opinion knowing different solutions to one problem extend your view to the language.