-
Notifications
You must be signed in to change notification settings - Fork 0
/
pokedex.js
82 lines (71 loc) · 2.44 KB
/
pokedex.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// función para conectar con el API.
const getPokemon = () => {
let pokeName = window.pokeName.value.toLowerCase();
fetch(`https://pokeapi.co/api/v2/pokemon/${pokeName}`)
.then((res) => {
if (res.status != "200") {
const pokeSprite = document.getElementById("pokeImg");
pokeSprite.src = "./assets/img/Error.png";
window.head.textContent = "Error";
} else {
return res.json();
}
})
.then((data) => {
// let pokeImg = data.sprites.front_default;
console.log(data);
updatePokemon(data);
getStats(data);
caracteristicas(data);
pokemonID(data);
});
};
//función para interactar con la parte frontal de la pokedex.
const updatePokemon = (data) => {
const pokeSprite = document.getElementById("pokeImg");
pokeSprite.src = data.sprites.other.home.front_default;
window.pokeId.textContent = data.name;
contenedor.removeAttribute("class");
};
//funciones para mostrar las Stats
const eliminarClase = (nombre) => {
const clase = Array.from(document.getElementsByClassName(nombre));
clase.forEach((element) => {
element.remove();
});
};
const getStats = (data) => {
const divStats = document.getElementById("Stats");
eliminarClase("statContenedor");
let Stats = data.stats;
Stats.forEach((element) => {
let stat = document.createElement("p");
stat.className = "stat";
let statNombre = document.createElement("p");
statNombre.className = "statNombre";
let statContenedor = document.createElement("div");
statContenedor.className = "statContenedor";
stat.innerText = element.base_stat;
statNombre.innerText = element.stat["name"].toUpperCase();
element.base_stat;
statContenedor.append(statNombre);
statContenedor.append(stat);
divStats.append(statContenedor);
});
};
//Funciones para mostrar caracteristicas
const caracteristicas = (data) => {
// tipo.removeAttribute("class");
tipo.innerText = data.types[0]["type"]["name"].toUpperCase();
contenedor.classList.add(data.types[0]["type"]["name"]);
if (data.types.length == 2) {
tipo2.innerText = data.types[1]["type"]["name"].toUpperCase();
// tipo2.removeAttribute("class");
} else tipo2.style.display = "none";
peso.innerText = data.weight;
altura.innerText = data.height;
};
const pokemonID = (data) => {
let id = data.id;
window.pokecon.value = id;
};