Skip to content

Commit 6a5f6db

Browse files
committed
Finished writing and checking getData function
also added API as a constant
1 parent 33fd02a commit 6a5f6db

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

script.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,31 @@ const defense = document.getElementById("defense");
1818
const specialAttack = document.getElementById("special-attack");
1919
const specialDefense = document.getElementById("special-defense");
2020
const speed = document.getElementById("speed");
21+
// api
22+
const api = "https://pokeapi.co/api/v2/pokemon/";
2123

2224
/* Fucntions */
2325
// returns an abject containing all the required fields.
24-
const getData = async () => {};
26+
const getData = async (query) => {
27+
const res = await fetch(api + query);
28+
const data = await res.json();
29+
console.log(data);
30+
const pokemon = {};
31+
pokemon.name = data.name;
32+
pokemon.id = data.id;
33+
pokemon.weight = data.weight;
34+
pokemon.height = data.height;
35+
pokemon.types = [];
36+
data.types.forEach((obj) => {
37+
pokemon.types.push(obj.type.name);
38+
});
39+
data.stats.forEach((obj) => {
40+
pokemon[obj.stat.name] = obj.base_stat;
41+
});
42+
// pokemon.sprite = data.sprites.front_default;
43+
pokemon.sprite = data.sprites.other.showdown.front_default;
44+
return pokemon;
45+
};
2546

2647
// updates the DOM with all the values recieved from API
2748
const update = async () => {};

0 commit comments

Comments
 (0)