12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- const API_KEY = "3516a881cfe62a50e7d405757def3a1a";
- async function get_temperature(city) {
- let parsed_city = encodeURIComponent(city);
- const url = `https://api.openweathermap.org/data/2.5/weather?q=${parsed_city}&appid=${API_KEY}`
- let get_result = fetch(url).then((result) => {
- let data = result.json().then((json_data) => {
- return json_data['main']['temp'];
- });
- return data;
- });
- return get_result;
- }
- async function main() {
- const city = "Poços de Caldas";
- const temp = await get_temperature(city);
- let divCity = document.querySelector("#city");
- if (!divCity) {
- console.log("Could not find city selector in document");
- return;
- }
- let divTemp = document.querySelector("#temp");
- if (!divTemp) {
- console.log("Could not find temp selector in document");
- return;
- }
- if (!temp) {
- console.log("Could not resolve temperature");
- return;
- }
- divCity.textContent = city;
- divTemp.textContent = (temp - 273.00).toFixed(2).toString() + " C";
- }
- main();
|