JavaScript Node.js Basics 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:

"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,631 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,091 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;