-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
123 lines (112 loc) · 3.2 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
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
"use strict";
let rawText = "";
let wildPokemon = new Map();
let eventPokemon = new Map();
const output = document.querySelector("output");
const getWildPokemonData = async () => {
// Replace ./data.json with your JSON feed
await fetch("./data/wild.txt")
.then((response) => {
return response.text();
})
.then((data) => {
// Work with JSON data here
let wildText = data;
for (let item of wildText.split(" ")) {
wildPokemon.set(item, item);
}
})
.catch((err) => {
console.log(err);
// Do something for an error here
});
};
getWildPokemonData();
const getEventPokemonData = async () => {
// Replace ./data.json with your JSON feed
await fetch("./data/event.txt")
.then((response) => {
return response.text();
})
.then((data) => {
// Work with JSON data here
let eventText = data;
for (let item of eventText.split(" ")) {
eventPokemon.set(item, item);
}
})
.catch((err) => {
console.log(err);
// Do something for an error here
});
};
getEventPokemonData();
const getBoostedPokemonData = async () => {
// Replace ./data.json with your JSON feed
await fetch("./data/boosted.txt")
.then((response) => {
return response.text();
})
.then((data) => {
// Work with JSON data here
rawText = data;
buildArray(rawText);
})
.catch((err) => {
console.log(err);
// Do something for an error here
});
};
getBoostedPokemonData();
const buildArray = (text) => {
const pokemonArray = [];
for (const item of text.split(" ")) {
pokemonArray.push(item);
}
getPokedexInfo(pokemonArray);
};
const getPokedexInfo = async (pokemonArray) => {
// Replace ./data.json with your JSON feed
await fetch("./data/pokedexdata.json")
.then((response) => {
return response.json();
})
.then((data) => {
// Work with JSON data here
sortArrayBasedOnPokedexData(data, pokemonArray);
})
.catch((err) => {
console.log(err);
// Do something for an error here
});
};
const sortArrayBasedOnPokedexData = (pokedexData, pokemonArray) => {
pokemonArray.sort(
(a, b) => pokedexData[a.split("-")[0]] - pokedexData[b.split("-")[0]]
);
buildUI(pokemonArray);
// pokemonArray.forEach((item) => console.info(pokedexData[item.split("-")[0]]));
// above line checks if sorting happened correctly
};
const buildUI = (pokemonArray) => {
for (let item of pokemonArray) {
let div = document.createElement("div");
item = item.replace(/mrmime/, "mr-mime").replace(/mimejr/, "mime-jr");
let img = document.createElement("img");
img.classList.add("pokemon");
item.includes("farfetchd") && img.classList.add("small");
wildPokemon.has(item) && div.classList.add("wild");
eventPokemon.has(item) && div.classList.add("event");
if (!item.trim()) continue;
img.src = `https://img.pokemondb.net/sprites/home/shiny/${item}.png`;
img.alt = item;
div.appendChild(img);
output.appendChild(div);
}
output.querySelectorAll("img").forEach((img) => {
img.addEventListener("click", () => {
img.style.scale = "1.2";
img.style.transformOrigin = "bottom";
});
});
};