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 Node.js Basics 2017 Create a Command Line Weather Application Parsing Data and Printing - Solution

Devon Stanton
Devon Stanton
7,793 Points

How do I change the temperature metric

I've gone with the other weather api. and it had the following to say in the documentation

full documentation: https://openweathermap.org/current#current_JSON

"main.temp Temperature. Unit Default: Kelvin, Metric: Celsius, Imperial: Fahrenheit."

but I don't know how to use the option in JavaScript? I can only think that now that I've made it an object I should be able to adjust the field unit to be Metric.

However even if I do that it wouldn't affect the values I'd just be changing the unit and not the actual temperature to reflect the unit.

2 Answers

Neil McPartlin
Neil McPartlin
14,662 Points

Devon, if you scroll down the link you provided to 'Units format', it explains that you just need to add &units=metric onto the end of your API call.

Charlie Gallentine
Charlie Gallentine
12,092 Points

When I have used openweathermap in the past, I have assigned the temperature to a variable and done the conversions to the variable to get it to different units. Sorry that it doesn't necessarily answer your question but it is a solution that I have found works!

I'm also not sure that I'm understanding your question entirely, but this should update all of the variables to have the right value each time a call is made to the API using $.getJSON() or comparable methods.

// Get the temperature from the API in units of Kelvin
temperatureKelvin = main.temp;

// Convert the temperature from Kelvin to degrees Celsius
temperatureCelsius = temperatureKelvin + 273.15;

// Convert the temperature from degrees Celsius to degrees Fahrenheit
temperatureFahrenheit = temperatureCelsius * 1.8 +32;