-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
70 lines (58 loc) · 2.09 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
"use strict";
const container = document.querySelector(".pokemon-container");
const pokemonID = document.querySelector(".pokemon-card__id");
const pokemonName = document.querySelector(".pokemon-card__name");
const pokemonImage = document.querySelector(".pokemon-card__image");
const pokemonHP = document.querySelector(".pokemon-card__hp");
let pokemonCount = 905;
const fetchPokemon = async () => {
// let id = 18;
// const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
// const data = await response.json();
// return data;
for (let id = 1; id <= 150; id++) {
await getPokemon(id);
}
};
const getPokemon = async (id) => {
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${id}`);
const data = await response.json();
const formatID = () => `${data.id.toString().padStart(3, "0")}`;
const formatWeight = () => `${parseFloat(data.weight / 10).toFixed(1)}`;
const formatHeight = () => `${parseFloat(data.height / 10).toFixed(1)}`;
const pokemon = {
id: formatID(),
hp: data.stats[0].base_stat,
name: data.name,
image: data.sprites.other.home.front_default,
type: data.types.map((type) => type.type.name),
weight: formatWeight(),
height: formatHeight(),
};
formatPokemon(pokemon);
};
fetchPokemon().catch((err) => {
console.error(err);
});
const formatPokemon = (pokemon) => {
const { id, hp, name, image, type, weight, height } = pokemon;
let html = `
<div class="pokemon-card">
<div class="pokemon-card__top_container">
<span class="pokemon-card__id">#${id}</span>
<span class="pokemon-card__hp"><img src=" ./images/hp-heart.png" />
${hp} / ${hp}
</span>
</div>
<h1 class="pokemon-card__name center-text">${name}</h1>
<img class="pokemon-card__image" src="${image}" />
<h2 class="pokemon-card__type center-text">${type.join(", ")}</h2>
<div class="pokemon-card__info center-text">
<h3 class="pokemon-card__info">
Height: ${height}m | Weight: ${weight}kg
</h3>
</div>
</div>
`;
container.innerHTML += html;
};