-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (42 loc) · 1.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
let urlBase = "https://api.openweathermap.org/data/2.5/weather";
let api_key = "f471801cf2abfd9efa74d90284f8ce85";
let difKelvin = 273.15;
document.getElementById("botonBusqueda").addEventListener("click", () => {
const ciudad = document.getElementById("ciudadEntrada").value;
if (ciudad) {
fetchDatosClima(ciudad);
}
});
function fetchDatosClima(ciudad) {
fetch(`${urlBase}?q=${ciudad}&appid=${api_key}`)
.then((data) => data.json())
.then((data) => mostrarDatosClima(data));
}
function mostrarDatosClima(data) {
console.log(data);
const divDatosClima = document.getElementById("datosClima");
divDatosClima.innerHTML = "";
const ciudadNombre = data.name;
const paisNombre = data.sys.country;
const temperatura = data.main.temp;
const humedad = data.main.humidity;
const descripcion = data.weather[0].description;
const icono = data.weather[0].icon;
ciudadTitulo = document.createElement("h2");
ciudadTitulo.textContent = `${ciudadNombre}, ${paisNombre}`;
const temperaturaInfo = document.createElement("p");
temperaturaInfo.textContent = `The temperature is: ${Math.floor(
temperatura - difKelvin
)}°C`;
const humedadInfo = document.createElement("p");
humedadInfo.textContent = `The humidty is: ${humedad}%`;
const iconoInfo = document.createElement("img");
iconoInfo.src = `https://openweathermap.org/img/wn/${icono}@2x.png`;
const descripcionInfo = document.createElement("p");
descripcionInfo.textContent = `The metereological description is: ${descripcion}`;
divDatosClima.appendChild(ciudadTitulo);
divDatosClima.appendChild(temperaturaInfo);
divDatosClima.appendChild(humedadInfo);
divDatosClima.appendChild(iconoInfo);
divDatosClima.appendChild(descripcionInfo);
}