-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
63 lines (54 loc) · 2.8 KB
/
script.js
File metadata and controls
63 lines (54 loc) · 2.8 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
async function getPokemonData(pokemonName) {
const apiUrl = `https://pokeapi.co/api/v2/pokemon/${pokemonName}`;
const response = await fetch(apiUrl);
if (!response.ok) throw new Error("Pokemon not found");
return response.json();
}
document.addEventListener("DOMContentLoaded", async () => {
const searchButton = document.getElementById("search-button");
const searchInput = document.getElementById("search-input");
searchInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
searchButton.click();
}
})
searchButton.addEventListener("click", async () => {
clearFields();
try {
const pokemonNameorId = searchInput.value;
if (!pokemonNameorId) return;
const pokemon = await getPokemonData(pokemonNameorId);
console.log(pokemon.stats);
console.log('Types:', pokemon.types);
console.log('Sprites', pokemon.sprites);
document.getElementById("pokemon-name").textContent = pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1).toLowerCase();
document.getElementById("sprite").src = pokemon.sprites.front_default;
document.getElementById("pokemon-id").textContent = `#${pokemon.id}`;
document.getElementById("weight").textContent = `Weight: ${pokemon.weight}`;
document.getElementById("height").textContent = `Height: ${pokemon.height}`;
document.getElementById("hp").textContent= `HP: ${pokemon.stats[0].base_stat}`;
document.getElementById("attack").textContent= `Attack: ${pokemon.stats[1].base_stat}`;
document.getElementById("defense").textContent= `Defense: ${pokemon.stats[2].base_stat}`;
document.getElementById("special-attack").textContent= `Special attack: ${pokemon.stats[3].base_stat}`;
document.getElementById("special-defense").textContent= `Special defense: ${pokemon.stats[4].base_stat}`;
document.getElementById("speed").textContent= `Speed: ${pokemon.stats[5].base_stat}`;
document.getElementById("types").textContent= `Types: ${pokemon.types[0].type.name.charAt(0).toUpperCase() + pokemon.types[0].type.name.slice(1).toLowerCase()}`;
} catch (error) {
alert('Pokémon not found');
}
function clearFields () {
document.getElementById("pokemon-name").textContent = '';
document.getElementById("sprite").src = '';
document.getElementById("pokemon-id").textContent = '';
document.getElementById("weight").textContent = '';
document.getElementById("height").textContent = '';
document.getElementById("hp").textContent= '';
document.getElementById("attack").textContent= '';
document.getElementById("defense").textContent= '';
document.getElementById("special-attack").textContent= '';
document.getElementById("special-defense").textContent= '';
document.getElementById("speed").textContent= '';
document.getElementById("types").textContent= '';
}
});
});