codex-lv3-may-2025

Kata 16: Fetch All Pokemon and Filter by Height

Concept: Filter pattern with fetched data (using for loop), then map to display

Challenge

Create a FilterPokemonByHeight component that:

  1. Fetches 20 pokemon from: https://pokeapi.co/api/v2/pokemon?limit=20
  2. Fetches each pokemon’s details (like Kata 15)
  3. Uses a for loop to filter pokemon where height is greater than 10
  4. Stores the filtered pokemon in state
  5. Uses .map() in JSX to display the filtered pokemon names

🔗 Practice on CodeSandbox

Expected Output

Displays only pokemon with height > 10:

Pokemon with height > 10:
- venusaur
- charizard
- blastoise
... (only tall pokemon)

Starter Code

import { useState, useEffect } from 'react';

export default function FilterPokemonByHeight() {
  const [filteredPokemon, setFilteredPokemon] = useState([]);
  
  useEffect(() => {
    async function fetchAndFilterPokemon() {
      // Step 1: Fetch list of pokemon
      const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
      const data = await response.json();
      
      // Step 2: Fetch all pokemon details (same as Kata 15)
      const allPokemon = [];
      for (let i = 0; i < data.results.length; i++) {
        const pokemonResponse = await fetch(data.results[i].url);
        const pokemon = await pokemonResponse.json();
        allPokemon.push(pokemon);
      }
      
      // Step 3: FILTER: Select only pokemon with height > 10 using for loop
      const tallPokemon = [];
      // Your filter for loop here
      
      setFilteredPokemon(tallPokemon);
    }
    
    fetchAndFilterPokemon();
  }, []);
  
  if (filteredPokemon.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Pokemon with height > 10:</h3>
      <ul>
        {filteredPokemon.map((pokemon, index) => (
          <li key={index}>{pokemon.name}</li>
        ))}
      </ul>
    </div>
  );
}

Hints

Solution

Click to reveal solution
import { useState, useEffect } from 'react';

export default function FilterPokemonByHeight() {
  const [filteredPokemon, setFilteredPokemon] = useState([]);
  
  useEffect(() => {
    async function fetchAndFilterPokemon() {
      // Step 1: Fetch list of pokemon
      const response = await fetch('https://pokeapi.co/api/v2/pokemon?limit=20');
      const data = await response.json();
      
      // Step 2: Fetch all pokemon details
      const allPokemon = [];
      for (let i = 0; i < data.results.length; i++) {
        const pokemonResponse = await fetch(data.results[i].url);
        const pokemon = await pokemonResponse.json();
        allPokemon.push(pokemon);
      }
      
      // Step 3: FILTER: Select only pokemon with height > 10 using for loop
      const tallPokemon = [];
      for (let i = 0; i < allPokemon.length; i++) {
        if (allPokemon[i].height > 10) {
          tallPokemon.push(allPokemon[i]);
        }
      }
      
      setFilteredPokemon(tallPokemon);
    }
    
    fetchAndFilterPokemon();
  }, []);
  
  if (filteredPokemon.length === 0) {
    return <div>Loading...</div>;
  }
  
  return (
    <div>
      <h3>Pokemon with height > 10:</h3>
      <ul>
        {filteredPokemon.map((pokemon, index) => (
          <li key={index}>{pokemon.name}</li>
        ))}
      </ul>
    </div>
  );
}

Concept Review

Using For Loop for Filtering

Using .map() Method for Rendering

Why Use Both?

Note: Learn more about:

Challenge Variation

Try filtering by:


Back to Kata Index