parent
9409685c9a
commit
11757efb6e
@ -1,64 +1,64 @@
|
|||||||
const poke_container = document.getElementById('poke-container')
|
const poke_container = document.getElementById("poke-container");
|
||||||
const pokemon_count = 150
|
const pokemon_count = 150;
|
||||||
const colors = {
|
const colors = {
|
||||||
fire: '#FDDFDF',
|
fire: "#FDDFDF",
|
||||||
grass: '#DEFDE0',
|
grass: "#DEFDE0",
|
||||||
electric: '#FCF7DE',
|
electric: "#FCF7DE",
|
||||||
water: '#DEF3FD',
|
water: "#DEF3FD",
|
||||||
ground: '#f4e7da',
|
ground: "#f4e7da",
|
||||||
rock: '#d5d5d4',
|
rock: "#d5d5d4",
|
||||||
fairy: '#fceaff',
|
fairy: "#fceaff",
|
||||||
poison: '#98d7a5',
|
poison: "#98d7a5",
|
||||||
bug: '#f8d5a3',
|
bug: "#f8d5a3",
|
||||||
dragon: '#97b3e6',
|
dragon: "#97b3e6",
|
||||||
psychic: '#eaeda1',
|
psychic: "#eaeda1",
|
||||||
flying: '#F5F5F5',
|
flying: "#F5F5F5",
|
||||||
fighting: '#E6E0D4',
|
fighting: "#E6E0D4",
|
||||||
normal: '#F5F5F5'
|
normal: "#F5F5F5",
|
||||||
}
|
};
|
||||||
|
|
||||||
const main_types = Object.keys(colors)
|
const main_types = Object.keys(colors);
|
||||||
|
|
||||||
const fetchPokemons = async () => {
|
const fetchPokemons = async () => {
|
||||||
for(let i = 1; i <= pokemon_count; i++) {
|
for (let i = 1; i <= pokemon_count; i++) {
|
||||||
await getPokemon(i)
|
await getPokemon(i);
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
|
|
||||||
const getPokemon = async (id) => {
|
const getPokemon = async (id) => {
|
||||||
const url = `https://pokeapi.co/api/v2/pokemon/${id}`
|
const url = `https://pokeapi.co/api/v2/pokemon/${id}`;
|
||||||
const res = await fetch(url)
|
const res = await fetch(url);
|
||||||
const data = await res.json()
|
const data = await res.json();
|
||||||
createPokemonCard(data)
|
createPokemonCard(data);
|
||||||
}
|
};
|
||||||
|
|
||||||
const createPokemonCard = (pokemon) => {
|
const createPokemonCard = (pokemon) => {
|
||||||
const pokemonEl = document.createElement('div')
|
const pokemonEl = document.createElement("div");
|
||||||
pokemonEl.classList.add('pokemon')
|
pokemonEl.classList.add("pokemon");
|
||||||
|
|
||||||
const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1)
|
const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1);
|
||||||
const id = pokemon.id.toString().padStart(3, '0')
|
const id = pokemon.id.toString().padStart(3, "0");
|
||||||
|
|
||||||
const poke_types = pokemon.types.map(type => type.type.name)
|
const poke_types = pokemon.types.map((type) => type.type.name);
|
||||||
const type = main_types.find(type => poke_types.indexOf(type) > -1)
|
const type = main_types.find((type) => poke_types.indexOf(type) > -1);
|
||||||
const color = colors[type]
|
const color = colors[type];
|
||||||
|
|
||||||
pokemonEl.style.backgroundColor = color
|
pokemonEl.style.backgroundColor = color;
|
||||||
|
|
||||||
const pokemonInnerHTML = `
|
const pokemonInnerHTML = `
|
||||||
<div class="img-container">
|
<div class="img-container">
|
||||||
<img src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${pokemon.id}.png"" alt="${name}">
|
<img src="https://assets.pokemon.com/assets/cms2/img/pokedex/full/${id}.png" alt="${name}">
|
||||||
</div>
|
</div>
|
||||||
<div class="info">
|
<div class="info">
|
||||||
<span class="number">#${id}</span>
|
<span class="number">#${id}</span>
|
||||||
<h3 class="name">${name}</h3>
|
<h3 class="name">${name}</h3>
|
||||||
<small class="type">Type: <span>${type}</span> </small>
|
<small class="type">Type: <span>${type}</span> </small>
|
||||||
</div>
|
</div>
|
||||||
`
|
`;
|
||||||
|
|
||||||
pokemonEl.innerHTML = pokemonInnerHTML
|
pokemonEl.innerHTML = pokemonInnerHTML;
|
||||||
|
|
||||||
poke_container.appendChild(pokemonEl)
|
poke_container.appendChild(pokemonEl);
|
||||||
}
|
};
|
||||||
|
|
||||||
fetchPokemons()
|
fetchPokemons();
|
||||||
|
Loading…
Reference in new issue