-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (36 loc) · 1.61 KB
/
script.js
File metadata and controls
49 lines (36 loc) · 1.61 KB
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
const api_key = '0a427845fb0850fd8e8fa8011f129fe3';
const urlBase = 'https://api.openweathermap.org/data/2.5/weather'
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) {
const divDatosClima = document.getElementById('datosClima')
divDatosClima.innerHTML = ''
const ciudadNombre = data.name;
const temperatura = data.main.temp;
const descripcion = data.weather[0].description;
const paisNombre = data.sys.country;
const humdedadClima = data.main.humidity;
const icono = data.weather[0].icon
const ciudadTitulo = document.createElement('h2')
ciudadTitulo.textContent = `${ciudadNombre}, ${paisNombre}`;
const temperaturaInfo = document.createElement('p')
temperaturaInfo.textContent = `La temperatura es: ${Math.floor(temperatura - difKelvin)}ºC, Humedad: ${humdedadClima}%`;
const iconoInfo = document.createElement('img')
iconoInfo.src = `https://openweathermap.org/img/wn/${icono}@2x.png`
const descripcionInfo = document.createElement('p')
descripcionInfo.textContent = `La descripcion es: ${descripcion}`;
divDatosClima.appendChild(ciudadTitulo);
divDatosClima.appendChild(temperaturaInfo);
divDatosClima.appendChild(iconoInfo)
divDatosClima.appendChild(descripcionInfo);
}