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 AJAX Basics (retiring) AJAX and APIs Create a callback function

Why is function created outside of $(document).ready() callback not accepted?

Hello, in the "Create a callback function" challenge, I'm ordered to create a function showWeather().

$(document).ready(function() {
  var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
  var data = {
    q : "Portland,OR",
    units : "metric"
  };
});

function showWeather(weatherReport) {}

When I create this function outside of the $(document).ready() callback, it's not accepted as a good answer. Why?

1 Answer

Chris Shaw
Chris Shaw
26,650 Points

Hi Peter,

The main reason is because the jQuery $.ready function contains code within a closure that your showWeather function has no access to as they're both within 2 different contexts, you would either need to move the variable assignments out of the $.ready function or move showWeather into it.

You can read more about closures over at MDN.