Oscar Rabadan
Oscar Rabadan

Reputation: 1

How can I extract this number from a weather api to convert it in Farenheit?

This is the function to take the temperature data from a weather API (among other data):

displayWeather: function (data) {
    const { name } = data;
    const { icon, description } = data.weather[0];
    const { temp, humidity } = data.main;
    const { speed } = data.wind;
    document.querySelector(".city").innerText = "El tiempo en " + name;
    document.querySelector(".icon").src =
        "http://openweathermap.org/img/wn/" + icon + ".png";
    document.querySelector(".description").innerText = description;
    document.querySelector(".temp").innerText = Math.trunc(temp) + "°C";
    document.querySelector(".humidity").innerText =
        "Humedad: " + humidity + "% ";
    document.querySelector(".wind").innerText =
        "Velocidad del viento: " + speed + " km/h";

The temp is the value I need to convert in Farenheit. The script is already done but I always get a NaN. I know there's a "°C" after that number, but even when I remove this to extract only the number I continue getting NaN.

Upvotes: 0

Views: 481

Answers (2)

rmike
rmike

Reputation: 11

Change your openweathermap API's units from imperial to metric:

  • For temperature in Fahrenheit use units=imperial
  • For temperature in Celsius use units=metric

Upvotes: 1

Oscar Rabadan
Oscar Rabadan

Reputation: 1

Thanks for your help. I finally solve the problem but figuring out which part was wrong and I discovered I wasn't using the correct line of code to extract the data and set it in the correct place.

Before:

 C = document.getElementById("ID").value;

Correct line:

C = document.querySelector(".temp").innerText;

Now that line of code finally takes the temp and I can turn it into Farenheit with:

F = (C * 9) / 5 + 32;

Sorry for the silly question. Now I just have to figure out how can I add the + "°C"

Upvotes: 0

Related Questions