-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
156 lines (134 loc) · 4.92 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const fetchPokemon = () => {
const pokeNameInput = document.getElementById("pokeName");
let pokeName = pokeNameInput.value;
pokeName = pokeName.toLowerCase();
const url = `https://pokeapi.co/api/v2/pokemon/${pokeName}`;
fetch(url).then((res) => {
if (res.status != "200") {
pokeImage("./img/pokemon.gif")
}
else {
return res.json();
}
}).then((data) => {
if (data) {
console.log(data);
let pokeImg = data.sprites.front_default;
pokeImage(pokeImg);
console.log(pokeImg);
let pokeNombre = data.name;
MostrarNombre(pokeNombre);
console.log(pokeNombre);
let pokeId = data.id;
MostrarId(pokeId);
console.log(pokeId);
let tipos = data.types.map(typ => typ.type.name);
console.log(tipos);
MostrarTipos(tipos);
let pokeEstadisticas =
data.stats[0].stat.name + ": " + data.stats[0].base_stat + "\n" +
data.stats[1].stat.name + ": " + data.stats[1].base_stat + "\n" +
data.stats[2].stat.name + ": " + data.stats[2].base_stat + "\n" +
data.stats[3].stat.name + ": " + data.stats[3].base_stat + "\n" +
data.stats[4].stat.name + ": " + data.stats[4].base_stat + "\n" +
data.stats[5].stat.name + ": " + data.stats[5].base_stat;
MostrarEstadisticas(pokeEstadisticas);
console.log(pokeEstadisticas);
let pokeMovs = "";
let cantMovs = Object.keys(data.moves).length;
for(let i = 0; i < cantMovs ; i++)
{
pokeMovs += data.moves[i].move.name + "\n";
}
MostrarMovimientos(pokeMovs);
console.log(pokeMovs);
}
});
}
const pokeImage = (url) => {
const pokePhoto = document.getElementById("pokeImg");
pokePhoto.src = url;}
const MostrarNombre = (pokeNombre) => {
const salidaNombre = document.getElementById("nombre");
salidaNombre.value =("Nombre: " + pokeNombre);
}
const MostrarId = (pokeId) => {
const salidaId = document.getElementById("id");
salidaId.value = ("# " + pokeId);
}
const MostrarTipos = (tipos) =>{
const salidaTipos = document.getElementById("tipos");
salidaTipos.value =("Tipo: " + tipos);
}
const MostrarEstadisticas = (pokeEstadisticas) => {
const salidaEstadisticas = document.getElementById("estadisticas");
salidaEstadisticas.innerHTML = pokeEstadisticas;
}
const MostrarMovimientos = (pokeMovs) => {
const salidaMovimientos = document.getElementById("movimientos")
salidaMovimientos.innerHTML =("movimientos: " + "\n" + pokeMovs);
}
/*work in progress...
const pokemonContainer = document.querySelector('.pokemon-container');
function fetchPokeid(id) {
fetch (`https://pokeapi.co/api/v2/pokemon/${id}/`)
.then((res) => res.json())
.then((data) => {
createPokemon(data);
});
}
function fetchPokeids(number) {
for (let i = 1; i <= number; i++) {
fetchPokeid(i);
}
}
function createPokemon(pokemon) {
const card = document.createElement('div');
card.classList.add('pokemon-card');
const spriteContainer = document.createElement('div');
spriteContainer.classList.add('sprite-container');
const sprite = document.createElement('img');
sprite.src = pokemon.sprites.front_default;
spriteContainer.appendChild(sprite);
const number = document.createElement('p')
number.textContent = `#${pokemon.id.toString().padStart(3,0)}`;
const name = document.createElement('p')
name.classList.add('name')
name.textContent = pokemon.name
const types = document.createElement('p')
types.src = pokemon.types
const stats = document.createElement('p')
stats.src = pokemon.stats[0].base_stats
card.appendChild(spriteContainer);
card.appendChild(number);
card.appendChild(name);
card.appendChild(types);
card.appendChild(stats);
pokemonContainer.appendChild(card);
}
fetchPokeids(9)
const typeColors = {
electric: '#FFEA70',
normal: '#B09398',
fire: '#FF675C',
water: '#0596C7',
ice: '#AFEAFD',
rock: '#999799',
flying: '#7AE7C7',
grass: '#4A9681',
psychic: '#FFC6D9',
ghost: '#561D25',
bug: '#A2FAA3',
poison: '#795663',
ground: '#D2B074',
dragon: '#DA627D',
steel: '#1D8A99',
fighting: '#2F2F2F',
default: '#2A1A1F',
};
const setCardColor = types => {
const colorOne = typeColors[types[0].type.name];
const colorTwo = types[1] ? typeColors[types[1].type.name] : typeColors.default;
pokeImg.style.background = `radial-gradient(${colorTwo} 33%, ${colorOne} 33%)`;
pokeImg.style.backgroundSize = ' 5px 5px';
} */