-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
61 lines (55 loc) · 2.13 KB
/
Copy pathapp.js
File metadata and controls
61 lines (55 loc) · 2.13 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
const input = document.getElementById("search-input");
const searchBtn = document.getElementById("search-button");
const namePokemon = document.getElementById("pokemon-name");
const imageContainer = document.getElementById("img-container");
const id = document.getElementById("pokemon-id");
const weight = document.getElementById("weight");
const height = document.getElementById("height");
const types = document.getElementById("types");
const baseStats = Array.from(document.querySelectorAll(".base-stats"));
const getInput = () => input.value
const regex = /[!@#$%^&*()-+_`~=]+/g;
const cleanInput = () => {
let userGiven = getInput();
userGiven = userGiven.toLowerCase();
userGiven = userGiven.replace(regex, "");
userGiven = userGiven.trim();
userGiven = userGiven.replaceAll(" ", "-");
userGiven = userGiven.replace("♀", "-f");
userGiven = userGiven.replace("♂", "-m");
return userGiven;
}
const getDetails = async () => {
namePokemon.textContent = "";
id.textContent = "";
weight.textContent = "";
height.textContent = "";
imageContainer.innerHTML = "";
types.innerHTML = "";
baseStats.forEach (p => {
p.textContent = "";
})
const insert = cleanInput();
try {
const response = await fetch(`https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/${insert}`);
const data = await response.json();
namePokemon.textContent = data.name.toUpperCase();
id.textContent = `#${data.id}`;
weight.textContent = `Weight: ${data.weight}`;
height.textContent = `Height: ${data.height}`;
imageContainer.innerHTML = `<img src="${data.sprites.front_default}" id="sprite" alt="${data.name}'s image">`;
let typesArr = data.types;
typesArr.forEach(obj => {
types.innerHTML += `<span class="card ${obj.type.name}">${obj.type.name.toUpperCase()}</span>`;
})
let i = 0;
baseStats.forEach(p => {
p.textContent = `${data.stats[i].base_stat}`
i++;
})
}
catch (error) {
alert("Pokémon not found");
}
}
searchBtn.addEventListener("click", getDetails);