-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon.js
141 lines (109 loc) · 4.73 KB
/
pokemon.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
let pokemon;
const id = getURLParameter("id");
let evolutionChain = [];
function getURLParameter(parameter) {
const urlParams = new URLSearchParams(window.location.search);
return urlParams.get(parameter);
}
async function getPokemonIDbyName(name) {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`);
const poke = await response.json();
return poke.id;
}
async function getPokemonData() {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
pokemon = await response.json();
const response1 = await fetch(pokemon.species.url);
const specie = await response1.json();
const response2 = await fetch(specie.evolution_chain.url);
let chain = await response2.json();
await getEvolutionChain(chain.id);
generateHTML();
}
async function getEvolutionChain(chainID) {
try {
evolutionChain = FullEvolutionChain.evolution_chain[chainID-1].species;
const chainCheck = evolutionChain.find(element => element.id == pokemon.id);
if(chainCheck == undefined){
console.log(evolutionChain);
evolutionChain = [];
}
evolutionChain.sort(function (a, b) {
if (a.order > b.order) {
return 1;
}
if (a.order < b.order) {
return -1;
}
return 0;
});
}
catch (e) {
;
}
}
function generateHTML() {
let title = pokemon.name;
title = title.charAt(0).toUpperCase() + title.slice(1);
document.querySelector("title").innerHTML = title;
document.querySelector(".container h1").innerHTML = pokemon.name;
const elementTypes = pokemon.types.map(typeInfo => typeInfo.type.name);
document.querySelector(".container h2").innerHTML = `${elementTypes.join(' | ')}`;
document.querySelector(".pokemon-image").alt = pokemon.name;
let imageID = pokemon.id+"";
switch(imageID.length){
case 1:
imageID = "00"+imageID;
break;
case 2:
imageID = "0"+imageID;
break;
default:
break;
}
document.querySelector(".pokemon-image").src = `https://assets.pokemon.com/assets/cms2/img/pokedex/full/${imageID}.png`;
document.querySelector(".pokemon-image").classList.add(pokemon.types[0].type.name);
document.querySelector("#xp-bar").innerHTML = "<span>"+pokemon.base_experience+"</span>";
document.querySelector("#xp-bar").style.width = ((pokemon.base_experience/608)*100).toString()+"%";
document.querySelector("#hp-bar").innerHTML = "<span>"+pokemon.stats[0].base_stat+"</span>";
document.querySelector("#hp-bar").style.width = ((pokemon.stats[0].base_stat/255)*100).toString()+"%";
document.querySelector("#atk-bar").innerHTML = "<span>"+pokemon.stats[1].base_stat+"</span>";
document.querySelector("#atk-bar").style.width = ((pokemon.stats[1].base_stat/255)*100).toString()+"%";
document.querySelector("#def-bar").innerHTML = "<span>"+pokemon.stats[2].base_stat+"</span>";
document.querySelector("#def-bar").style.width = ((pokemon.stats[2].base_stat/255)*100).toString()+"%";
document.querySelector("#sp-atk-bar").innerHTML = "<span>"+pokemon.stats[3].base_stat+"</span>";
document.querySelector("#sp-atk-bar").style.width = ((pokemon.stats[3].base_stat/255)*100).toString()+"%";
document.querySelector("#sp-def-bar").innerHTML = "<span>"+pokemon.stats[4].base_stat+"</span>";
document.querySelector("#sp-def-bar").style.width = ((pokemon.stats[4].base_stat/255)*100).toString()+"%";
document.querySelector("#speed-bar").innerHTML = "<span>"+pokemon.stats[5].base_stat+"</span>";
document.querySelector("#speed-bar").style.width = ((pokemon.stats[5].base_stat/255)*100).toString()+"%";
if(evolutionChain.length > 0){
let accumulator = "";
for(let i = 0; i < evolutionChain.length; i++){
accumulator += `
<div class="evo-box">
<a href="pokemon.html?id=${evolutionChain[i].id}">
<img class="evo-image" alt="${evolutionChain[i].name}" src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/${evolutionChain[i].id}.png" />
<p>${evolutionChain[i].name}</p>
`;
accumulator += `
</a>
</div>
`;
if(i+1 < evolutionChain.length){
accumulator += `
<p><i class="arrow"></i></p>
`;
}
}
document.querySelector("#evolution-chain").innerHTML = accumulator;
}
else{
document.querySelector("#evolution").style.display = "none";
}
}
function goBack() {
location.href = "index.html";
}
getPokemonData()
.then();