-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
28 lines (22 loc) · 1002 Bytes
/
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
const getPokemonUrl = id => `https://pokeapi.co/api/v2/pokemon/${id}`
const generatePokemonPromises = () => Array(150).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://pokeres.bastionbot.org/images/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)