-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (46 loc) · 1.67 KB
/
Copy pathscript.js
File metadata and controls
75 lines (46 loc) · 1.67 KB
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
const pokemonName = document.querySelector('.pokemon_name');
const pokemonNumber = document.querySelector('.pokemon_number');
const pokemonImage = document.querySelector('.pokemon_image');
const form = document.querySelector('.form');
const input = document.querySelector('.input_search');
const bPrev = document.querySelector('.btn-prev');
const bNext = document.querySelector('.btn-next');
let searchPokemon = 1;
const fetchPokemon = async (pokemon) => {
const apiResponse = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemon}`);
if (apiResponse.status == 200){
const data = await apiResponse.json();
return data;
}
}
const renderPokemon = async (pokemon) => {
pokemonName.innerHTML = 'Carregando...';
const data = await fetchPokemon(pokemon);
if (data){
pokemonImage.style.display ='block';
pokemonName.innerHTML = data.name;
pokemonNumber.innerHTML = data.id;
pokemonImage.src = data['sprites']['versions']['generation-viii']['icons']['front_default'];
searchPokemon = data.id;
input.value = '';
} else {
pokemonImage.style.display = 'none';
pokemonName.innerHTML = 'Not founded ;c';
pokemonNumber.innerHTML = '';
}
}
form.addEventListener('submit', (event) => {
event.preventDefault();
renderPokemon(input.value.toLowerCase());
});
bPrev.addEventListener('click', () => {
if (searchPokemon >1 ){
searchPokemon -=1
renderPokemon(searchPokemon);
}
});
bNext.addEventListener('click', () => {
searchPokemon +=1
renderPokemon(searchPokemon);
});
renderPokemon(searchPokemon);