-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (42 loc) · 1.63 KB
/
Copy pathscript.js
File metadata and controls
49 lines (42 loc) · 1.63 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
const searchButton = document.getElementById('searchButton');
const pokemonNameInput = document.getElementById('pokemonName');
const pokemonDataDiv = document.getElementById('pokemonData');
const fetchPokemon = async () => {
const query = pokemonNameInput.value.toLowerCase().trim();
if (!query) {
displayError('Please enter a Pokémon name or ID.');
return;
}
try {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${query}`);
if (!response.ok) {
throw new Error('Pokémon not found.');
}
const data = await response.json();
displayPokemon(data);
} catch (error) {
displayError(error.message);
}
};
const displayPokemon = (data) => {
const { name, id, sprites, types, abilities } = data;
const image = sprites.front_default || '';
const typeElements = types.map(typeInfo => `<span class="type">${typeInfo.type.name}</span>`).join('');
const abilityElements = abilities.map(abilityInfo => `<li>${abilityInfo.ability.name}</li>`).join('');
pokemonDataDiv.innerHTML = `
<img src="${image}" alt="${name}">
<h2>${name} (#${id})</h2>
<div class="info"><strong>Type:</strong> ${typeElements}</div>
<div class="info"><strong>Abilities:</strong> <ul>${abilityElements}</ul></div>
`;
};
const displayError = (message) => {
pokemonDataDiv.innerHTML = `<p class="error">${message}</p>`;
};
searchButton.addEventListener('click', fetchPokemon);
// Allow pressing "Enter" to search
pokemonNameInput.addEventListener('keyup', (event) => {
if (event.key === 'Enter') {
fetchPokemon();
}
});