-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
32 lines (26 loc) · 1.01 KB
/
app.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
const getPokemonUrl = id => `https://pokeapi.co/api/v2/pokemon/${id}`
const generatePokemonPromises = () =>
Array(1008)
.fill()
.map((_, index) =>
fetch(getPokemonUrl(index + 1)).then(response => response.json())
)
const generateHTML = pokemons =>
pokemons.reduce((accumulator, { name, id, types }) => {
const elementTypes = types.map(typeInfo => typeInfo.type.name)
accumulator += `
<li class="card ${elementTypes[0]}">
<img class="card-image" alt="${name}"
src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${id}.png" />
<h2 class="card-title">${id}. ${name}</h2>
<p class"card-subtitle">${elementTypes.join(' | ')}</p>
</li>
`
return accumulator
}, '')
const insertPokemonsIntoPage = pokemons => {
const ul = document.querySelector('[data-js="pokedex"]')
ul.innerHTML = pokemons
}
const pokemonPromises = generatePokemonPromises()
Promise.all(pokemonPromises).then(generateHTML).then(insertPokemonsIntoPage)