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

Heidi S Wolff
Heidi S Wolff
12,757 Points

Creating an Anonymous Callback Function

https://teamtreehouse.com/library/callback-functions-in-javascript/introduction-to-callback-functions/creating-an-anonymous-callback-function

function log() { console.log("Hello World!"); }

functionRunner(log () { console.log("Hello World!"); }}

What am I doing wrong?

5 Answers

Anonymous functions are unnamed functions. All the challenge is looking for you to do is to create an unnamed function and place it into the functionRunner call as a callback instead of log.

// This is a named function with a name of 'log'
function log() {
  console.log("Hello World!");
}

functionRunner(log); // This is adding a callback of 'log'

// This is an anonymous function since it has no name
function() {
  console.log("Hello World!");
}

// We have no way to reference an anonymous function since 
// it has no name so we manually move the above code into the
// function call as a callback like so:
functionRunner(function() {
  console.log("Hello World!");
})

This gives the functionRunner an anonymous function as a callback, thereby removing the need for the named log function

Edit: And the stuff Steven Parker has mentioned below

Steven Parker
Steven Parker
221,450 Points

It looks like there's 3 issues:

  • the anonymous function won't use the name "log" ("anonymous" = "no name")
  • but it will need the keyword "function"
  • and the final closing parenthesis seems to have changed into a close bracket
Jose Luis Jiménez Sastre
Jose Luis Jiménez Sastre
12,314 Points

The challenge asks you to convert log in to an anonymous function and pass the anonymous function directly into functionRunner. In order to do it, you have to copy log function into functionRunner, but without naming it. Therefore, once you've done that you don't need the log function anymore.

functionRunner(function () { console.log("Hello World!"); });

Steven Parker
Steven Parker
221,450 Points

True, but leaving the original named function there won't affect passing the challenge.

Heidi S Wolff
Heidi S Wolff
12,757 Points

Thank you everyone! It finally worked!

Heidi S Wolff
Heidi S Wolff
12,757 Points

The animation challenge has stumped me.

const section = document.getElementById("animateMe");

function startAnimation() { (startAnimation, 2000); document.getElementById = "spin"; })

Steven Parker
Steven Parker
221,450 Points

You might want to create a new question, show your code there and also link to the course page.

If you start a question from the "get help" button it should include these for you.