Created
September 22, 2022 01:27
-
-
Save Tribhuwan-Joshi/ce464d560d89586f405140359d8504bf to your computer and use it in GitHub Desktop.
Weather App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const app = (() => { | |
function giveData(day) { | |
const res = {}; | |
res.weather = day.weather[0].main; | |
res.temp = day.main.temp; | |
res.feelslike = day.main.feels_like; | |
res.windSpeed = day.wind.speed; | |
res.humidity = day.main.humidity; | |
res.pop = `${day.pop * 100}%`; | |
res.icon = day.weather[0].icon; | |
const dt = day.dt_txt.split(" "); | |
res.date = format(new Date(dt[0]), "dd MMM yyyy"); | |
res.time = new Date().toLocaleTimeString("en-US", { | |
hour: "numeric", | |
minute: "numeric", | |
hour12: true, | |
}); | |
return res; | |
} | |
async function getWeather(query) { | |
const res = await fetch( | |
`http://api.openweathermap.org/data/2.5/forecast?q=${query}&APPID=d48a46383954cbdee3198804107fd92d`, | |
{ | |
mode: "cors", | |
} | |
); | |
const data = await res.json(); | |
const cityName = data.city.name; | |
const regionNames = new Intl.DisplayNames(["en"], { type: "region" }); | |
const countryName = regionNames.of(data.city.country); | |
const day1 = giveData(data.list[2]); | |
day1.city = cityName; | |
day1.countryName = countryName; | |
const day2 = giveData(data.list[10]); | |
const day3 = giveData(data.list[18]); | |
console.log(day1); | |
console.log(day2); | |
console.log(day3); | |
} | |
return { | |
getWeather, | |
giveData, | |
}; | |
})(); | |
const city = "Sweden"; | |
console.log(app.getWeather(city)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment