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 Callback Functions in JavaScript Introduction to Callback Functions Creating an Anonymous Callback Function

Valerie Laughlin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Valerie Laughlin
Front End Web Development Techdegree Graduate 16,110 Points

In the app.js file, functionRunner executes a callback, log. Convert log in to an anonymous function and pass the anonym

I am really stuck on this javascript question that seems to have a simple answer.

The exercise is telling me to convert "log" into an anonymous function which I believe is :

function log() {

}

Then I get error messages saying that callback still has a name

I am also confused with how to pass the anonymous function directly to functionRunner

Is it?

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

Thank you for your help

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

functionRunner(log);
index.html
<!DOCTYPE html>
<html lang="en">
    <head>
        <title></title>
        <link rel='stylesheet' href='styles.css'>
    </head>
    <body>
        <section>
            <p>Open your browser's console to see the results</p>
        </section>
        <script src='runner.js'></script>
        <script src='app.js'></script>
    </body>
</html>
runner.js
function functionRunner(callback) { 
  callback();
}

Hi, Valerie! An anonymous function looks like:

function() {}

So, as you can see "anonymous" means no name like "log". So, your anonymous function will look like:

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

And the same function pased to the runner will look like:

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

8 Answers

For everyone still having problems with this - just copy and paste my code into the section.

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

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

that works + you can remove the curl braces because you don't need them here.

this challenge is bugged.

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

// I convert the above function into an anonymous function and pass it as a parameter.

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

Yet it is still flagged as incorrect.

I found the issue. To pass this task, you need to delete the original, named, function after converting it to an anonymous function and passing it as a parameter. The challenge only asks you to convert the function into an anonymous function and pass it as a parameter, hence the confusion.

I can't get this to work either. Either with my own code or with the previous answerer's code.

It still doesn't work Andrew Chalkley

function functionRunner(callback) { callback(); }

functionRunner(() => console.log("Hello World!"));

Bummer: The callback still has a name!

JQMIGRATE: Migrate is installed with logging active, version 1.4.1 head_vendor-5b9ebdc4a02e1b2e5ee764c861e9bbb31c76ac261c1825fea64a849cc73d8085.js:1185 [bugsnag] Ignoring cross-domain or eval script error. See docs: https://tinyurl.com/y94fq5zm t @ head_vendor-5b9ebdc4a02e1b2e5ee764c861e9bbb31c76ac261c1825fea64a849cc73d8085.js:1185 d3cxv97fi8q177.cloud…58b85e71.min.js:392 Uncaught SyntaxError: Unexpected end of input head_vendor-5b9ebdc4a02e1b2e5ee764c861e9bbb31c76ac261c1825fea64a849cc73d8085.js:1185 [bugsnag] Ignoring cross-domain or eval script error. See docs: https://tinyurl.com/y94fq5zm t @ head_vendor-5b9ebdc4a02e1b2e5ee764c861e9bbb31c76ac261c1825fea64a849cc73d8085.js:1185 d3cxv97fi8q177.cloud…58b85e71.min.js:392 Uncaught SyntaxError:

Unexpected end of input app.js:7 Hello World!