-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokeapi.js
62 lines (49 loc) · 1.89 KB
/
pokeapi.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
53
54
55
56
57
58
59
60
61
62
let id = 25;
let api_url = 'https://pokeapi.co/api/v2/';
let api_endpoint = 'pokemon';
let pokemon_id = document.getElementById('pokemon_id');
let pokemon_name = document.getElementById('pokemon_name');
let pokemon_data = document.getElementById('pokemon_data');
let alert = document.getElementById('error');
document.addEventListener('DOMContentLoaded', cargarDatos);
function cargarDatos() {
fetch(api_url + api_endpoint + '/' + id)
.then(response => response.json())
.then(pokemon => {
pokemon_id.textContent = id;
pokemon_name.textContent = pokemon.name;
pokemon_data.replaceChildren();
pokemon_data.append(filaTabla('Experiencia', pokemon.base_experience));
pokemon_data.append(filaTabla('Altura', pokemon.height / 10 + ' m'));
pokemon_data.append(filaTabla('Peso', pokemon.weight / 10 + ' kg'));
pokemon_data.append(filaTabla('Imagen', '<img class="artwork" alt="Artwork oficial" src="' + pokemon.sprites.other['official-artwork'].front_default + '"/>'));
alert.classList.add('d-none');
})
.catch(error => {
pokemon_id.textContent = '?';
pokemon_name.textContent = 'Error';
alert.textContent = error;
alert.classList.remove('d-none');
});
}
function filaTabla(titulo, contenido) {
let tr = document.createElement('tr');
let th = document.createElement('th');
th.classList.add("table-dark");
th.textContent = titulo;
tr.append(th);
let td = document.createElement('td');
td.innerHTML = contenido;
tr.append(td);
return tr;
}
let boton_menos = document.getElementById('boton_menos');
let boton_mas = document.getElementById('boton_mas');
boton_menos.addEventListener('click', () => {
id--;
cargarDatos();
});
boton_mas.addEventListener('click', () => {
id++;
cargarDatos();
});