Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

On about line 18, make the anonymous function with the code 'window.didExecute = true' execute.

var anonymousFunction = function(){};

  anonymousFunction = (function () {
    window.didExecute = true;
  });

8 Answers

For anyone who is still confused, all you need to do is:

      var anonymousFunction = function() {};

      (function () {
        window.didExecute = true;
      }) ();

http://helephant.com/2008/08/23/javascript-anonymous-functions/

omg guys we've been breaking our heads for nothing loll All it asks is to make the self executing function execute which all it was missing were the parentheses, it never said to make the self executing function execute the anonymousFunction variable! hahah Seems a bit of a mixup of explanation and variable confusion. I was stuck on this for a good hour or so! XD Hope this helps anyone confused about it.

Thank you for saving my sanity! I really appreciate seeing this screenshot.

Kelvin Knighton
Kelvin Knighton
6,168 Points

thanks man, i was starting to hyperventilate.

Thank you for this. I got confused since the task says “on about line 18”.

This is weird but it works. Thank you!

Incredibly helpful! I was stuck for so long!

Michael Paccione
Michael Paccione
9,017 Points

Due to your help my computer was spared from a 30 foot drop to the silicon splitting concrete below. My box thanks you!

Elisabeth Meyer
Elisabeth Meyer
6,231 Points

You have my sincere gratitude, Fred. I was nearly two cups of tea into this problem before I saw your comment. Hah. Thank you.

Jamil Jones
Jamil Jones
10,852 Points

Sure did!! Thanks my Friend!

just put the open and close parenthesis at the end of the function

(anonymousFunction = (function () { window.didExecute = true; })();

nope

Thanks lots - seeing the code along with screenshot above really helped.

In the video prior to the this task, Jim placed 2 random arguments in the empty parenthesis to immediately pass the anonymous function like so:

(function () { 
    var a, b, c;
    // ...
    console.log('from anon function')
})(arg1, arg2)

My question is, why would this not work in the task? Also, what is the difference when using empty vs. filled parenthesis in an anonymous function?

Kevin Murphy
Kevin Murphy
24,380 Points

Kelvin, I struggled for awhile with this challenge question and after figuring it out, had (have) the same questions as you. I think further elaboration/clarification is required in the video when that topic is introduced. Particularly what role the arguments (or lack of arguments) play in an anon function when it's used as an object (as per your screenshot).

Kevin Murphy
Kevin Murphy
24,380 Points

Kelvin, I share same questions as you. Struggled for awhile with challenge question. I believe the role of args in an anon function used as an object could benefit from further elaboration in the video.

I second that, I passed in (1, 'hello') like Jim did at the end of video, but instead the answer is to just put () - I'm none the wiser as to why that works.

I wondered the same thing: if empty parentheses works, why does passing it some sort of value not work?

davinder rehal
davinder rehal
888 Points

var anonymousFunction = function() {};

  (function () {
    window.didExecute = true;
  })();

don't forget the first open parenthesis before "anonymousFunction". I saw that you didn't put it there.

I don't get it

sorry, I've wrong. Actually, you don't need to put the 'anonymousFunction' before the function itself. On line 18, the function is already there, just put the open and close parenthesis at the end of it. Like this: (function () { window.didExecute = true; })(); see? No 'anomFunction' before it. Try it, and if it doesn't work, sorry but I don't know what else it can be.

Josh Flowers
Josh Flowers
7,010 Points

Eldy Voon, Carlos is correct. All you need to do is add a set of empty parenthesis as there are no arguments to pass into it.

Elias Thanasopoulos
Elias Thanasopoulos
32,286 Points

What it worked for me in the 2nd task is the below:

(function () {

window.didExecute=true;

})()(anonymousFunction);

This is worked (function () {

window.didExecute=true;

})()();