index.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. const API_KEY = "3516a881cfe62a50e7d405757def3a1a";
  2. async function get_temperature(city) {
  3. let parsed_city = encodeURIComponent(city);
  4. const url = `https://api.openweathermap.org/data/2.5/weather?q=${parsed_city}&appid=${API_KEY}`
  5. let get_result = fetch(url).then((result) => {
  6. let data = result.json().then((json_data) => {
  7. return json_data['main']['temp'];
  8. });
  9. return data;
  10. });
  11. return get_result;
  12. }
  13. async function main() {
  14. const city = "Poços de Caldas";
  15. const temp = await get_temperature(city);
  16. let divCity = document.querySelector("#city");
  17. if (!divCity) {
  18. console.log("Could not find city selector in document");
  19. return;
  20. }
  21. let divTemp = document.querySelector("#temp");
  22. if (!divTemp) {
  23. console.log("Could not find temp selector in document");
  24. return;
  25. }
  26. if (!temp) {
  27. console.log("Could not resolve temperature");
  28. return;
  29. }
  30. divCity.textContent = city;
  31. divTemp.textContent = (temp - 273.00).toFixed(2).toString() + " C";
  32. }
  33. main();