Stuck on this for a couple of days now - still new to JS. I am trying to make the variable's values from inside the function available outside the function. I have tried using global_this and it doesn't work. The values show up fine in the console log and inner HTML - however when I try to use them anywhere else I get undefined.
enter code here
function x() {fetch('http://api.openweathermap.org/data/2.5/weather?
lat=33.778726&lon=-118.377751&appid=c8ba55aeb3b4aeb0add9badde352f0df&units=imperial')
.then( response => response.json())
.then( data => openweather(data))
.catch( err => console.log(err))
console.log("hack")
function openweather(data) {
swamprat = Object.values(data)
windspeed = data.wind.speed;
winddegree = data.wind.deg;
airtemp = data.main.temp;
airpressure = data.main.pressure;
knots= (windspeed * 0.868976);
windspeed = parseInt(windspeed);
winddegree = parseInt(winddegree);
airtemp = parseInt(airtemp);
airpressure = parseInt(airpressure)
knots= parseInt(knots);
console.log("Wind Speed: " + windspeed)
console.log("Wind Direction: " + winddegree)
console.log("Temperature: " + airtemp)
console.log("Air Pressure: " + airpressure)
console.log("Knots: " + knots);
document.getElementById("windspeedx").innerHTML = [windspeed]
document.getElementById("winddirectionx").innerHTML = [winddegree]
document.getElementById("airtemperaturex").innerHTML = [airtemp]
document.getElementById("airpressurex").innerHTML = [airpressure]
document.getElementById("knotsx").innerHTML = [knots]
}
}
console.log(windspeed)
x()
setInterval(x,15000)