-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
38 lines (33 loc) · 1.03 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
document.querySelector("#search").addEventListener("click", getPokemon);
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
function lowerCaseName(string) {
return string.toLowerCase();
}
function getPokemon(e) {
const name = document.querySelector("#pokemonName").value;
const pokemonName = lowerCaseName(name);
fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonName}`)
.then((response) => response.json())
.then((data) => {
document.querySelector(".pokemonBox").innerHTML = `
<div>
<img
src="${data.sprites.other["official-artwork"].front_default}"
alt="Pokemon name"
/>
</div>
<div class="pokemonInfos">
<h1>${capitalizeFirstLetter(data.name)}</h3>
<p>Weight: ${data.weight}</p>
</div>`;
})
.catch((err) => {
document.querySelector(".pokemonBox").innerHTML = `
<h4>Pokemon not found 😞</h4>
`;
console.log("Pokemon not found", err);
});
e.preventDefault();
}