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 trialHeidi S Wolff
12,757 PointsCreating an Anonymous Callback Function
function log() { console.log("Hello World!"); }
functionRunner(log () { console.log("Hello World!"); }}
What am I doing wrong?
5 Answers
Joe Beltramo
Courses Plus Student 22,191 PointsAnonymous 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
229,732 PointsIt 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
12,314 PointsThe 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
229,732 PointsTrue, but leaving the original named function there won't affect passing the challenge.
Heidi S Wolff
12,757 PointsThank you everyone! It finally worked!
Heidi S Wolff
12,757 PointsThe animation challenge has stumped me.
const section = document.getElementById("animateMe");
function startAnimation() { (startAnimation, 2000); document.getElementById = "spin"; })
Steven Parker
229,732 PointsYou 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.