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

S Ananda
S Ananda
9,474 Points

Stuck on quiz in Ajax basics

My last line of code is wrong. Everything worked up until then. Below are the instructions, the error code and my answer. I obviously missed something on how to use the .text() even after reading up on in in jquery.api.

Instructions: Finally, to insert the temperature into the div, use jQuery's text() method. You'll need to pass the temperature into this method. The Open Weather Map API returns an object with the property main; that property holds another object with the property "temp". To access the temperature, use dot syntax, like this: weatherReport.main.temp

Error message: Bummer! You need to chain the text() method to your selection. Add a dot after the jQuery selection, followed by 'text()'.

My code.

$(document).ready(function() { var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather'; var data = { q : "Portland,OR", units : "metric" }; function showWeather(weatherReport){ $("#temperature").html(statusHTML); $(‘#main”).text(); }; });

weather.js
$(document).ready(function() {
  var weatherAPI = 'http://api.openweathermap.org/data/2.5/weather';
  var data = {
    q : "Portland,OR",
    units : "metric"
  };
  function showWeather(weatherReport){
   $("#temperature").html(statusHTML);
    $('#main').text();
  };
});
index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>What's the Weather Like?</title>
  <script src="jquery.js"></script>
  <script src="weather.js"></script>
</head>
<body>
  <div id="main">
    <h1>Current temperature: <span id="temperature"></span>&deg;</h1>
  </div>
</body>
</html>

1 Answer

You are close.

The object that is returned into the showWeather method as weatherReport has a property called main on it. In order to access this property temp, you need to to use dot notation as shown in the instructions: weatherReport.main.temp.

Then instead of using the jQuery method .html, it is wanting you to use the .text method.

So you have gotten this far:

function showWeather(weatherReport){
   $("#temperature").html(statusHTML);
};

Try replacing the html method with text:

$("#temperature").text(statusHTML);

And then try replacing statusHTML with weatherReport.main.temp

S Ananda
S Ananda
9,474 Points

Thanks Joe, that fixed the problem. Sometimes I'm still having problems knowing what to plug in where , but it is getting easier.

Never stop practicing, things get easier :]

S Ananda
S Ananda
9,474 Points

I've been at JS for awhile now. Just recently things are starting to make sense, not all things obviously, but a lot more of them. Hoping to get some practical experience with JS tomorrow at a hackathon. That's why I'm trying to finish this module. They want me to do some programming with JSON. Lucky for me these folks are very patient and helpful. It's with a Code for America brigade, so they are trying to come up with tasks at different levels of experience. Really a great group.

Thanks again. Now I just have to get through the last challenge...

Well best of luck to you at your Hackathon. Most developers want you to success so it's not completely surprising they are patient and helpful. You can get through the last challenge. Just pay attention to the videos and what you are asked to do. Don't forget to play 'offline' (in your own environment) with the things you learn!

S Ananda
S Ananda
9,474 Points

Thanks for the encouragement Joe. I will pay close attention and I am playing some offline. Will do more shortly, as I'm trying to crank through a few tutorials. Once done with that I'll be doing a lot of playing, as I have a membership site I want to build.