-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
24 lines (23 loc) · 848 Bytes
/
main.js
File metadata and controls
24 lines (23 loc) · 848 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function randomPokemon() {
const pokeApi = "https://pokeapi.co/api/v2/pokemon/";
const randomId = Math.floor(Math.random() * 1034 + 1);
const pokemon = fetch(`${pokeApi}${randomId}`).then((response) =>
response.json(),
);
return pokemon;
}
const botonRandom = document.querySelector(`#random`);
const display = document.querySelector(`.display`);
botonRandom.addEventListener("click", async function () {
const pokemon = await randomPokemon();
console.log(pokemon);
const container = document.createElement(`div`);
container.id = pokemon.id;
const title = document.createElement(`h3`);
title.innerText = pokemon.name;
const img = document.createElement(`img`);
img.src = pokemon.sprites.other.showdown.front_default;
container.appendChild(img);
container.appendChild(title);
display.appendChild(container);
});