-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
138 lines (113 loc) · 3.47 KB
/
script.js
File metadata and controls
138 lines (113 loc) · 3.47 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* Luke Melaia - Pokédex
*/
let pokemon = [];
let filter = null;
async function fetchPokemon(limit = 151) {
try {
const response = await fetch(
`https://pokeapi.co/api/v2/pokemon?limit=${limit}`
);
const data = await response.json();
return await Promise.all(
data.results.map(async (pokemon) => {
const pokemonResponse = await fetch(pokemon.url);
return pokemonResponse.json();
})
);
} catch (error) {
console.error("Error fetching data:", error);
}
}
function createPokemon(pokemon) {
const card = document.createElement("div");
card.className = "pokemon-card";
const img = document.createElement("img");
img.src = pokemon.sprites.front_default;
img.alt = pokemon.name;
const number = document.createElement("p");
number.className = "pokemon-number";
number.textContent = `#${pokemon.id.toString().padStart(3, "0")}`;
const name = document.createElement("h3");
name.className = "pokemon-name";
name.textContent =
pokemon.name.charAt(0).toUpperCase() + pokemon.name.slice(1);
const types = document.createElement("div");
types.className = "pokemon-types";
pokemon.types.forEach((typeInfo) => {
const typeButton = document.createElement("p");
typeButton.className = `type-label ${typeInfo.type.name}`;
typeButton.textContent =
typeInfo.type.name.charAt(0).toUpperCase() + typeInfo.type.name.slice(1);
types.appendChild(typeButton);
});
card.appendChild(img);
card.appendChild(number);
card.appendChild(name);
card.appendChild(types);
return card;
}
function displayAllPokemon(pokemonList) {
const container = document.getElementById("container");
container.innerHTML = "";
pokemonList.forEach((pokemon) => {
const card = createPokemon(pokemon);
container.appendChild(card);
});
}
function filterAndSort(searchTerm, sortOrder, typeFilter) {
let filtered = pokemon.filter((pokemon) => {
// Name
if (pokemon.name.toLowerCase().includes(searchTerm.toLowerCase())) {
return true;
}
// ID
if (pokemon.id.toString().includes(searchTerm)) {
return true;
}
// Type
for (k in pokemon.types) {
if (pokemon.types[k].type.name.includes(searchTerm.toLowerCase())) {
return true;
}
}
});
filtered.sort((a, b) => {
if (sortOrder === "asc") {
return a.id - b.id;
} else {
return b.id - a.id;
}
});
return filtered;
}
function clearSearch() {
document.getElementById("search").value = "";
var event = new Event("input", {
bubbles: true,
});
document.getElementById("search").dispatchEvent(event);
}
async function init() {
if (pokemon.length === 0) {
pokemon = await fetchPokemon();
}
displayAllPokemon(pokemon);
const search = document.getElementById("search");
const sort = document.getElementById("sort");
const clear = document.getElementById("clear");
function updateDisplay() {
const searchTerm = search.value.trim();
const currentSortOrder = sort.value;
const filteredAndSortedPokemon = filterAndSort(
searchTerm,
currentSortOrder,
filter
);
displayAllPokemon(filteredAndSortedPokemon);
}
search.addEventListener("input", updateDisplay);
sort.addEventListener("change", updateDisplay);
clear.addEventListener("click", clearSearch);
}
window.addEventListener("load", init);