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

Convert log in to an anonymous function and pass the anonymous function directly into functionRunner.

//The app.js file originally looked like this:
function log() {
  console.log("Hello World!");
}

functionRunner(log);

//My edit on the app.js file was this:
function log( callback ) {
  callback();
}

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

functionRunner(log);

//I am confused as to where to fix this... The error I received was as follows: "There was an error with your code: TypeError: 'undefined' is not a function (evaluating 'callback()')"
app.js
function log( callback ) {
  callback();
}

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();
}

1 Answer

Simon Coates
Simon Coates
4,967 Points

It seemed to accept:

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

But that's not an anonymous function... is it??

Simon Coates
Simon Coates
4,967 Points

MDN (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions) describes the following as an anonymous function and I saw similar example at w3cschools.

var myFunction = function() {
    statements
}

I think most people would immediately start to think about arrow syntax. But I tried this an it worked and it didn't seem to like my use of arrow syntax, so I guess that was what it wanted. The language i used was "It seemed to accept", which avoids committing to anything beyond passing the challenge.