-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
80 lines (66 loc) · 1.93 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
const button = document.getElementById('catch');
const content = document.querySelector('.content');
const pokeTypes = {
normal: 'normal',
fighting: 'fighting',
flying: 'flying',
poison: 'poison',
ground: 'ground',
rock: 'rock',
bug: 'bug',
ghost: 'ghost',
steel: 'steel',
fire: 'fire',
water: 'water',
grass: 'grass',
eletric: 'electric',
psychic: 'psychic',
ice: 'ice',
dragon: 'dragon',
dark: 'dark',
fairy: 'fairy',
unknown: 'unknown',
shadow: 'shadow'
}
let pokemons = [];
async function fetchPokemonData(pokemon){
let url = pokemon.url;
fetch(url)
.then(response => response.json())
.then(async function(pokeData){
await renderPokemon(pokeData);
})
};
async function fetchPokemons() {
fetch('https://pokeapi.co/api/v2/pokemon?limit=151').then(response => response.json())
.then(async function(allpokemon){
allpokemon.results.map(async function(pokemon){
await fetchPokemonData(pokemon);
return;
});
});
}
async function renderPokemon(pokeData) {
button.remove();
let card = document.createElement('div');
card.classList.add('card');
let name = document.createElement('p');
name.innerText = pokeData.name;
let types = document.createElement('span');
let oneType = pokeData.types.map(type => type.type.name)
oneType.forEach(element => {
let type = document.createElement('span');
type.classList.add(element);
type.innerText = element;
types.append(type);
});
let frontImage = document.createElement('img');
frontImage.classList.add('front');
frontImage.src = pokeData.sprites.front_default;
let shinyImage = document.createElement('img');
shinyImage.classList.add('remove', 'shiny');
shinyImage.src = pokeData.sprites.front_shiny;
card.append(frontImage, shinyImage, name, types);
content.appendChild(card);
}
button.addEventListener('click',fetchPokemons);