Can you solve this?

by rsutton 22. January 2010 07:27

There is an interesting problem I solved today that I thought I might share.  The basic idea can be solved by figuring out how to solve the following problem.


Consider this JavaScript
funcs = []
for(var i=0;i<10;i++)
       funcs[i] = function() { alert(i); }
funcs[4]();

This will alert "10".  Can you change it to alert "4"?  The key to solving this problem is being able to create a function that stores i and then will evaluate the alert later.

Also
funcs[0] alerts "0"
funcs[1] alerts "1"
...
funcs[9] alerts "9"

Get the idea?  Good luck!

Tags:

Development

Comments are closed

Powered by BlogEngine.NET 1.4.5.0
Theme by Extensive SEO

Profile

Member of the Church of Jesus Christ of Latter-Day Saints, Developer and IT Professional