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

Patrick Ludwig
Patrick Ludwig
2,462 Points

It is impossible to pass both tasks on this challenge because they contradict each other

Task one asks you to "set the variable 'anonymous function' to be an anonymous function without any code to be executed." In order to pass this first part of the challenge, you have to remove the part that says 'window.didExecute = true'. However, then Task 2 tells you to "make the anonymous function with the code 'window.didExecute = true' execute." If you add this code back in though, it says "you are no longer passing Task 1." Either we have the code in there or not, you cannot do it both ways.

index.html
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>JavaScript Foundations: Functions</title>
    <style>
      html {
        background: #FAFAFA;
        font-family: sans-serif;
      }
    </style>
  </head>
  <body>
    <h1>JavaScript Foundations</h1>
    <h2>Functions: Anonymous Functions</h2>
    <script>
      var anonymousFunction=(function () {

      });
      anonymousFunction();
    </script>
  </body>
</html>

2 Answers

Hey Patrick,

The two tasks are separate from each other using two different anonymous functions. When the first task refers to an anonymous function, it didn't say an anonymous self invoking function (such as the one below the variable). It refers to an anonymous function. An anonymous function is any function that doesn't have a name i.e.

var myfunc = function () {};

myfunc is an anonymous function. myfunc contains a reference to the anonymous function but it is not named. If you want to name the function, you add the name you want after function like so:

var myfunc = function myfunc () {};

Now the myfunc function has a name.

So, you want to set anonymousFunction to an anonymous function, like the first block of code I showed you. And then, set the self invoking anonymous function to execute, which is done by putting parenthesis outside of the function like you see below.

      var anonymousFunction = function () {};

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

You're very welcome, Patrick! :)