Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Created November 26, 2018 11:05
Show Gist options
  • Save harrisonmalone/0b9cd5e249b230acda43993a59f734b7 to your computer and use it in GitHub Desktop.
Save harrisonmalone/0b9cd5e249b230acda43993a59f734b7 to your computer and use it in GitHub Desktop.
pokedex api code day 1
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<h1>Pokemon list</h1>
<div id="pokemon-list">
</div>
<script src="index.js"></script>
</body>
</html>
const renderPokemon = ((pokemon) => {
const list = document.querySelector('#pokemon-list')
pokemon.forEach((poke, index) => {
const panel = `
<div>
<a href="poke-info.html?${index + 1}">
<p>${poke.name}</p>
</a>
</div>
`
list.insertAdjacentHTML('beforeend', panel)
})
})
const url = 'https://pokeapi.co/api/v2/pokemon/'
fetch(url)
.then((resp) => resp.json())
.then((json) => {
const actualPokemon = json.results.splice(0, 151)
renderPokemon(actualPokemon)
})
.catch((err) => console.error(err))
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<div id="poke-info"></div>
<script src="poke-info.js"></script>
</body>
</html>
const renderPokemon = (poke) => {
const panel = document.querySelector('#poke-info')
const pokeInfo = `
<h1>${poke.id} | ${poke.name}</h2>
<p>Weight: ${poke.weight} pounds</p>
<p>Height: ${poke.height} feet</p>
<img src="${poke.sprites.front_default}">
`
panel.insertAdjacentHTML('beforeend', pokeInfo)
renderAbilities(poke, panel)
renderMoves(poke)
}
const renderAbilities = (poke, panel) => {
panel.insertAdjacentHTML('afterend', '<div id="abilities"></div>')
const abilityList = document.querySelector('#abilities')
const listHeading = document.createElement('h2')
listHeading.innerText = `${poke.name} abilities`
abilityList.prepend(listHeading)
const list = document.createElement('ul')
abilityList.append(list)
poke.abilities.forEach(function(abilityObj) {
const listItem = document.createElement('li')
listItem.innerText = abilityObj.ability.name
list.append(listItem)
})
}
const renderMoves = (poke) => {
const abilityList = document.querySelector('#abilities')
abilityList.insertAdjacentHTML('afterend', '<div id="moves"></div>')
const moves = document.querySelector('#moves')
const movesHeading = document.createElement('h2')
movesHeading.innerText = `${poke.name} moves`
moves.prepend(movesHeading)
const list = document.createElement('ul')
moves.append(list)
poke.moves.forEach(function(movesObj) {
const listItem = document.createElement('li')
listItem.innerText = movesObj.move.name
list.append(listItem)
})
}
const getId = () => {
const queryParams = window.location.search
const id = queryParams.substr(1)
return id
}
const fetchInfo = () => {
const id = getId()
const url = `https://pokeapi.co/api/v2/pokemon/${id}/`
fetch(url)
.then(resp => resp.json())
.then(json => renderPokemon(json))
}
fetchInfo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment