Project 1

Week2-3 - My Pokemon

Step 1: Find a interesting topic

Website: https://pokeapi.co/

Step 2: Organize the functions of the website, and fetch APIs

Step 4: Add code for Layout & Buttons

<html><head>
<title>My Pokemon Collection</title>
<style>
body {
font-family: "Arial", sans-serif;
background-color: #f4f4f4;
margin: 0;
padding: 20px;
text-align: center;
}

label {
font-size: 18px;
margin-right: 10px;
}

input {
padding: 8px;
font-size: 16px;
}

button {
padding: 10px;
font-size: 16px;
background-color: #4caf50;
color: white;
border: none;
cursor: pointer;
}

button:hover {
background-color: #45a049;
}

#pokemonList {
margin-top: 20px;
display: flex;
flex-wrap: wrap;
justify-content: center;
}

#pokemonList a {
margin: 10px;
padding: 10px;
background-color: #3498db;
color: white;
text-decoration: none;
border-radius: 5px;
transition: background-color 0.3s ease;
line-height: 4;
}

#pokemonList a:hover {
background-color: #2980b9;
}
</style>
</head>


Step 3: Fetch APIs

  1. The first API

fetch(
          `https://pokeapi.co/api/v2/pokemon?limit=${numberOfPokemons}&offset=0`
        )
          .then((response) => {
            if (response.ok) {
              return response.json();
            } else {
              throw new Error("Network response was not ok.");
            }
          })
          .then((data) => {
            data.results.forEach((pokemon, index) => {
              pokemonList.innerHTML += `<div><a href="#" onclick="getPokemonDetails(${
                index + 1
              })">${pokemon.name}</a></div>`;
            });
          })
          .catch((error) => {
            document.getElementById("pokemonList").innerText =
              "Error fetching Pokemon list: " + error.message;
          });
      }

2. The second API

function getPokemonDetails(pokemonId) {
        var pokemonUrl = `https://pokeapi.co/api/v2/pokemon/${pokemonId}/`;

        fetch(pokemonUrl)
          .then((response) => {
            if (response.ok) {
              return response.json();
            } else {
              throw new Error("Network response was not ok.");
            }
          })
          .then((data) => {
            const abilities = data.abilities.map((ability) => {
              return `<div><a href="#" onclick="getAbilityDetails('${ability.ability.url}')">${ability.ability.name}</a></div>`;
            });

3. The third API

function getAbilityDetails(abilityUrl) {
        fetch(abilityUrl)
          .then((response) => {
            if (response.ok) {
              return response.json();
            } else {
              throw new Error("Network response was not ok.");
            }
          })
          .then((abilityData) => {
            document.getElementById(
              "cryptochart"
            ).innerHTML = `<div><b>Ability Name:</b> ${abilityData.name}</div>`;
            document.getElementById(
              "cryptochart"
            ).innerHTML += `<div><b>Effect:</b> ${abilityData.effect_entries[0].effect}</div>`;
          })
          .catch((error) => {
            document.getElementById("cryptochart").innerText =
              "Error fetching Ability data: " + error.message;
          });
      }