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 JavaScript Foundations Functions Anonymous Functions

Tolulope Ajulo
Tolulope Ajulo
3,380 Points

Anonymous Function Task

Hi I don't know what I am doing wrong here.

My solution is:

var anonymousFunction = (function () {

window.didExecute = true;

})();

I've made the function anonymous any help will be appreciated.

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Tolulope;

Saw your response that you need assistance with Task 2.

You are on the right track with the code you posted, you just need to adjust a few things. Give this a try:

 <script>
      var anonymousFunction = function() {}; // Task 1

      (function () {
        window.didExecute = true;
      }) (); // Task 2 - add the () to the end of the line.
    </script>

Happy coding,

Ken

Tolulope Ajulo
Tolulope Ajulo
3,380 Points

Got it now thanks so much Ken

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Tolulope;

Let's take a look at this. We are given the following script code to start the Anonymous Functions challenge:

    <script>
      var anonymousFunction;

      (function () {
        window.didExecute = true;
      });
    </script>

We are asked to: Set the variable 'anonymousFunction' to be an anonymous function without any code to be executed.

We are already given the line var anonymousFunction; so we just need to set that var to a function without any code to be executed. We can do that with the following code:

var anonymousFunction= function() {};

That does exactly what the task is asking us to do in a very efficient manner.

In your code:

var anonymousFunction = (function () {

window.didExecute = true;

})();

Your var anonymousFunction has code inside of it, in addition to some extra parenthesis which are causing the issue.

Happy coding,

Ken

Tolulope Ajulo
Tolulope Ajulo
3,380 Points

Hi Ken,

Thanks for your response, it is actually the second question of the task I am struggling i.e. On about line 18, make the anonymous function with the code 'window.didExecute = true' execute

This is my solution:

var anonymousFunction = (function () {

window.didExecute = true;

})();
Vittorio Somaschini
Vittorio Somaschini
33,371 Points

Hello Tolulope Ajulo.

The challenge is making the function anonymous and without any code inside it.

So, first remove all the code inside the function, that means all code inside the { } needs removing.

Plus you type a couple of extra ( here and there. Please doublecheck them.

An empty anonymous function should look like this:

var doSomething = function() {};
Tolulope Ajulo
Tolulope Ajulo
3,380 Points

Hi Vittorio,

Thanks for your response, it is actually the second question of the task I am struggling i.e. On about line 18, make the anonymous function with the code 'window.didExecute = true' execute

Thanks alot