Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Code Challenge: Anonymous Functions

I just can't pass this code challenge on anonymous functions? Any suggestions?

"Set the variable 'anonymousFunction' to be an anonymous function without any code to be executed."

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

Thanks.

10 Answers

I passed the challenge with the following code:

var anonymousFunction = function() {} anonymousFunction();

So I've passed both parts of the challenge with the following code and a lot of help.

    var anonymousFunction = function (){
    };

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

That varable above needs to be set to an anonymous function

function () {};

That is what an anonymous function looks like. And all you need to do is set that variable equal to that anonymous function.

Does that help?

This is my code so far:

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

That function we will leave alone. Instead of joining "var anonymousFunction;" with above. Do this:

var anonymousFunction = function () {};

Creating a entirely new anonymous function and setting that as the value of anonymousFunction.

Great! glad to see you figured it out!

I now can't pass the second part of that challange:

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

I get this warning:

"Bummer! You're missing the parentheses at the end of the function '(function(){window.didExecute=true;})'"

If I try anything different I just get another warning:

"Oops! It looks like Task 1 is no longer passing".

Any ideas?

What was your code when got that first warning?

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

Great job!

I typed the exact same thing for this problem 15 times, to the point I was just copying and pasting my same answer in over and over again at a complete loss as to why it wouldnt take it. Changed nothing. And on the 15 try, seconds away from launching my laptop across the room it accepted it. What the hell. Seriously. Not impressed. It kept telling me I was missing the parentheses that were are the end, even though they were there. :( What a waste of an hour. Sometimes I wonder about this method. Normally its due to a silly typo or something but this time that was definitely not the case.