-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (34 loc) · 1.27 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
// Funktion zum Großschreiben des ersten Buchstabens
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
}
// Basis-URL der API
async function getPokemon() {
const baseUrl = "https://pokeapi.co/api/v2/pokemon";
// Input-Feld-Wert auslesen
const query = document.getElementById("searchInput").value.trim();
// Ist ein Input vorhanden?
if (!query) {
console.error("Bitte gib einen Pokémon-Namen oder eine ID ein.");
return;
}
// Die URL wird zusammengebaut
const url = `${baseUrl}/${query.toLowerCase()}`;
// Ergebnis-Container
const resultDiv = document.getElementById("result");
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Pokémon nicht gefunden. Status: ${response.status}`);
}
const json = await response.json();
resultDiv.innerHTML = `
<h2>${capitalizeFirstLetter(json.name)} (#${json.id})</h2>
<img src="${json.sprites.front_default}" alt="${json.name}">
<p>Typ: ${json.types.map(type => type.type.name).join(", ")}</p>
`;
} catch (error) {
resultDiv.innerHTML = `<p class="text-danger">${error.message}</p>`;
console.error(error.message);
}
}