Complete solution (100% reference)
index.html
- Main HTML pagescript.js
- JavaScript logicstyles.css
- Styling and layoutpackage.json
- Dependenciessetup.sh
- Setup scriptREADME.md
- Instructions (below)๐ก Download the ZIP, extract it, and follow the instructions below to get started!
Master the art of JSON parsing and the fetch API with Pokemon! This comprehensive project teaches you how to work with complex, nested JSON data structures through the engaging Pokemon API.
By completing this activity, you will:
python3 -m http.server 8000
TODO 1: Basic Pokemon Search โญ
TODO 2: Random Pokemon Generator โญ
TODO 3: Raw JSON Display โญโญ
TODO 4: Stats Parser โญโญโญ
TODO 5: Moves Parser โญโญโญ
TODO 6: Types Parser โญโญโญ
TODO 7: Pokemon Comparison โญโญโญโญ
TODO 8: Team Builder โญโญโญโญ
The Pokemon API returns complex JSON with this structure:
{
"id": 25,
"name": "pikachu",
"height": 4,
"weight": 60,
"types": [
{
"slot": 1,
"type": {
"name": "electric",
"url": "https://pokeapi.co/api/v2/type/13/"
}
}
],
"stats": [
{
"base_stat": 35,
"effort": 0,
"stat": {
"name": "hp",
"url": "https://pokeapi.co/api/v2/stat/1/"
}
}
],
"moves": [...], // 100+ moves
"sprites": {
"front_default": "image_url"
}
}
// Accessing nested properties
const pokemonName = data.name;
const firstType = data.types[0].type.name;
const hpStat = data.stats[0].base_stat;
// Processing arrays
const typeNames = data.types.map(type => type.type.name);
const statValues = data.stats.map(stat => stat.base_stat);
// Filtering arrays
const firstTenMoves = data.moves.slice(0, 10);
const attackMoves = data.moves.filter(move =>
move.move.name.includes('attack')
);
Start with TODO 1 - Pokemon Search
// API endpoint to use:
`https://pokeapi.co/api/v2/pokemon/${pokemonName}`
// Basic pattern:
const response = await fetch(url);
const data = await response.json();
currentPokemonData = data;
displayPokemonCard(data);
Complete TODO 2 - Random Pokemon
// Generate random ID:
const randomId = Math.floor(Math.random() * 1010) + 1;
// Use same API pattern as TODO 1
Finish TODO 3 - Raw JSON
// Format JSON for display:
JSON.stringify(currentPokemonData, null, 2)
TODO 4: Stats Parsing
// Extract stats array:
const stats = currentPokemonData.stats;
// Process each stat:
stats.forEach(stat => {
const name = stat.stat.name;
const value = stat.base_stat;
// Display them
});
TODO 5: Moves Parsing
// Get first 10 moves:
const moves = currentPokemonData.moves.slice(0, 10);
// Extract move names:
const moveNames = moves.map(move => move.move.name);
TODO 6: Types Parsing
// Extract types:
const types = currentPokemonData.types;
// Create styled type badges:
types.map(type =>
`<span class="type-${type.type.name}">${type.type.name}</span>`
);
TODO 7: Comparison
// Fetch multiple Pokemon:
const [data1, data2] = await Promise.all([
fetch(`/pokemon/${pokemon1}`).then(r => r.json()),
fetch(`/pokemon/${pokemon2}`).then(r => r.json())
]);
TODO 8: Team Builder
// Generate multiple random IDs:
const ids = [1, 2, 3].map(() =>
Math.floor(Math.random() * 1010) + 1
);
// Fetch all at once:
const teamData = await Promise.all(
ids.map(id => fetch(`/pokemon/${id}`).then(r => r.json()))
);
Basic Functionality:
Data Parsing:
Advanced Features:
"Pokemon not found" errors:
"Cannot read property" errors:
data?.types?.[0]?.type?.name
Empty displays:
Your project is complete when:
const { name, id } = pokemon;
map()
, filter()
, slice()
async/await
, Promise.all()
pokemon?.sprites?.front_default
console.log()
, console.table()
Upon completion, you'll have mastered:
This foundation prepares you perfectly for the next activity where we'll explore API authentication and work with multiple APIs simultaneously!
Need Help?
console.log()
liberally to inspect data structuresRemember: The Pokemon API is free, fast, and doesn't require authentication - perfect for learning! ๐ฏโก