Skip to content

Commit 1edc0a6

Browse files
Merge pull request Nivedita-coder#7 from leonardomath/main
Simple pokedex
2 parents 7c1c6c1 + 26cb675 commit 1edc0a6

5 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎pokedex/css/bulma.min.css‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pokedex/css/style.css‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
.int {
2+
display: flex;
3+
align-items: center;
4+
flex-direction: column;
5+
padding: 10px;
6+
}
7+
8+
.int input {
9+
max-width: 240px;
10+
margin: 10px 0;
11+
}
12+
13+
.int button {
14+
display: block;
15+
}
16+
17+
#show {
18+
text-align: center;
19+
}

‎pokedex/icon.png‎

50 KB
Loading

‎pokedex/index.html‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Simple Pokedex</title>
7+
<link rel="stylesheet" href="./css/bulma.min.css">
8+
<link rel="stylesheet" href="./css/style.css">
9+
<link rel="icon" type="image/png" href="./icon.png" />
10+
</head>
11+
<body>
12+
<div class="container int">
13+
<h1 class="title">Pokedex</h1>
14+
<input type="text" class="input" placeholder="Pokemon name" id="pokemon_name">
15+
<button class="button is-warning">Search</button>
16+
<div id="show">
17+
</div>
18+
</div>
19+
<script src="./scripts/pokemon.js"></script>
20+
</body>
21+
</html>

‎pokedex/scripts/pokemon.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
let name, pokemon, sprite, abilities
2+
const showDiv = document.querySelector('#show')
3+
4+
async function getPokemon() {
5+
6+
name = document.querySelector('#pokemon_name').value
7+
8+
try {
9+
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${name}`)
10+
const data = await response.json()
11+
12+
sprite = data.sprites.front_default
13+
type = data.types[0].type.name
14+
abilities = data.abilities
15+
console.log(data, data.abilities)
16+
renderPokemon(sprite, type, abilities)
17+
} catch (error) {
18+
alert('pokemon not found')
19+
}
20+
}
21+
22+
function renderPokemon(sprite, pokemonType, abilities) {
23+
const divImg = document.querySelector('img')
24+
divImg ? divImg.remove() : ''
25+
const img = document.createElement('img')
26+
img.setAttribute('src', sprite)
27+
28+
const typeElement = document.querySelector('#type')
29+
typeElement ? typeElement.remove() : ''
30+
const type = document.createElement('p')
31+
type.setAttribute('id', 'type')
32+
const textType = document.createTextNode(`Type: ${pokemonType}`)
33+
type.appendChild(textType)
34+
35+
const ul = document.querySelector('ul')
36+
ul ? ul.remove() : ''
37+
const ulElement = document.createElement('ul')
38+
39+
abilities.map((ab, index) => {
40+
const li = document.createElement('li')
41+
const textLi = document.createTextNode(`Ability ${index+1}: ${ab.ability.name}`)
42+
li.appendChild(textLi)
43+
ulElement.appendChild(li)
44+
})
45+
46+
showDiv.appendChild(img)
47+
showDiv.appendChild(type)
48+
showDiv.appendChild(ulElement)
49+
}
50+
51+
const button = document.querySelector('button')
52+
button.addEventListener('click', getPokemon)

0 commit comments

Comments
 (0)