|
| 1 | +const pokeContainer = document.getElementById("poke-container"); |
| 2 | +const pokemonCount = 150; |
| 3 | +const colors = { |
| 4 | + fire: "#FDDFDF", |
| 5 | + grass: "#DEFDE0", |
| 6 | + electric: "#FCF7DE", |
| 7 | + water: "#DEF3FD", |
| 8 | + ground: "#f4e7da", |
| 9 | + rock: "#d5d5d4", |
| 10 | + fairy: "#fceaff", |
| 11 | + poison: "#98d7a5", |
| 12 | + bug: "#f8d5a3", |
| 13 | + dragon: "#97b3e6", |
| 14 | + psychic: "#eaeda1", |
| 15 | + flying: "#F5F5F5", |
| 16 | + fighting: "#E6E0D4", |
| 17 | + normal: "#F5F5F5", |
| 18 | +}; |
| 19 | +const mainTypes = Object.keys(colors); |
| 20 | + |
| 21 | +const createPokemonCard = (pokemon) => { |
| 22 | + const pokemonElement = document.createElement("div"); |
| 23 | + pokemonElement.classList.add("pokemon"); |
| 24 | + const name = pokemon.name[0].toUpperCase() + pokemon.name.slice(1); |
| 25 | + const id = pokemon.id.toString().padStart(3, "0"); |
| 26 | + const pokeTypes = pokemon.types.map((type) => type.type.name); |
| 27 | + const type = mainTypes.find((type) => pokeTypes.indexOf(type) > -1); |
| 28 | + const color = colors[type]; |
| 29 | + pokemonElement.style.backgroundColor = color; |
| 30 | + const pokemonInnerHTML = ` |
| 31 | + <div class="img-container"> |
| 32 | + <img |
| 33 | + src="https://pokeres.bastionbot.org/images/pokemon/${pokemon.id}.png" |
| 34 | + alt="" |
| 35 | + /> |
| 36 | + </div> |
| 37 | + <div class="info"> |
| 38 | + <span class="number">#${id}</span> |
| 39 | + <h3 class="name">${name}</h3> |
| 40 | + <small class="type">Type: <span>${type}</span></small> |
| 41 | + </div> |
| 42 | + `; |
| 43 | + pokemonElement.innerHTML = pokemonInnerHTML; |
| 44 | + pokeContainer.appendChild(pokemonElement); |
| 45 | +}; |
| 46 | + |
| 47 | +const getPokemon = async (id) => { |
| 48 | + const url = `https://pokeapi.co/api/v2/pokemon/${id}`; |
| 49 | + const res = await fetch(url); |
| 50 | + const data = await res.json(); |
| 51 | + createPokemonCard(data); |
| 52 | +}; |
| 53 | + |
| 54 | +const fetchPokemons = async () => { |
| 55 | + for (let i = 1; i < pokemonCount; i++) { |
| 56 | + await getPokemon(i); |
| 57 | + } |
| 58 | +}; |
| 59 | + |
| 60 | +fetchPokemons(); |
0 commit comments