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 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,676 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.