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

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

Code challenge bug for anonymous functions part 2?

in part 1 the code passes, but in part 2 when I add the "windows.didExecute = true;" back and I get error message "It looks like Task 1 is no longer passing."

I see absolutely zero reason why this is the case. Is the code challenge being buggy?

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 () {
        window.didExecute = true;
              };
    </script>
  </body>
</html>

2 Answers

Shawn Flanigan
PLUS
Shawn Flanigan
Courses Plus Student 15,803 Points

Sarah,

This one's a bit tricky. The challenge isn't asking you to move the second anonymous function into your first anonymous function, it's just asking you to execute the second function. The technique they're looking for is called an Immediately-Invoked Function Expression, or you might sometimes see them called Self-Executing Anonymous Functions. Instead of naming the function or declaring it as a variable, we just write the function and execute it immediately.

To do this we simply add another set of parentheses (and any required parameters) toward the end of our function. The syntax feels a little strange, but it should look something like this:

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

I know this wasn't a great explanation, but I hope it helps a little!

Sarah A. Morrigan
Sarah A. Morrigan
14,329 Points

OK... this section has too many trick challenges.

So I wrote two separate lines (the first task I put it in one single line for a clean visual, and it worked)

Second line should follow the proper IIFE construct with (function() {}())

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

But the error message keeps telling me that the parentheses are missing at the end after "true;}"

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,803 Points

Yeah...not sure why, but it passes like this:

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

Doesn't quite look like the proper syntax, does it? I'll investigate a little.

Shawn Flanigan
Shawn Flanigan
Courses Plus Student 15,803 Points

Looks like either syntax is technically valid...I guess the challenge just wants the latter.