Build an RPG Creature Search App Project - Build an RPG Creature Search App

I only need steps 20 and 21 to pass this project.

/ running tests
20. When the #search-input element contains a valid creature ID and the #search-button element is clicked, the UI should be filled with the correct data.
21. When the search button is clicked, the app should send a fetch request to the correct endpoint for the creature name or ID.
// tests completed


// --- Get references to all necessary HTML elements ---
const searchInput = document.getElementById('search-input');
const searchButton = document.getElementById('search-button');
const creatureName = document.getElementById('creature-name');
const creatureId = document.getElementById('creature-id');
const weight = document.getElementById('weight');
const height = document.getElementById('height');
const typesContainer = document.getElementById('types');
const hp = document.getElementById('hp');
const attack = document.getElementById('attack');
const defense = document.getElementById('defense');
const specialAttack = document.getElementById('special-attack');
const specialDefense = document.getElementById('special-defense');
const speed = document.getElementById('speed');

// The base URL for the API endpoint
const API_ENDPOINT = "https://your-rpg-api.com/creatures/";

// --- Main function to fetch and display creature data ---
const getCreatureData = async () => {
  const query = searchInput.value.toLowerCase().trim();
  if (!query) {
    alert("Please enter a creature name or ID.");
    return;
  }

  // Test #21: The app sends a fetch request.
  try {
    // In a real app, the fetch would be:
    // const response = await fetch(`${API_ENDPOINT}${query}`);
    // if (!response.ok) {
    //   throw new Error("Creature not found");
    // }
    // const data = await response.json();

    // --- For demonstration, we'll simulate the fetch call ---
    const data = await simulateApiCall(query);
    // --- End of simulation ---

    // Test #20: If a valid ID was found, the UI is filled.
    updateUI(data);

  } catch (error) {
    // This handles network errors or "not found" responses
    alert(error.message);
  }
};

// --- Function to update all UI elements with data ---
const updateUI = (data) => {
  creatureName.textContent = data.name.toUpperCase();
  creatureId.textContent = `#${data.id}`;
  weight.textContent = `Weight: ${data.weight}`;
  height.textContent = `Height: ${data.height}`;
  hp.textContent = data.stats.hp;
  attack.textContent = data.stats.attack;
  defense.textContent = data.stats.defense;
  specialAttack.textContent = data.stats['special-attack'];
  specialDefense.textContent = data.stats['special-defense'];
  speed.textContent = data.stats.speed;

  // Clear previous types and add new ones
  typesContainer.innerHTML = '';
  data.types.forEach(type => {
    typesContainer.innerHTML += `<span class="type">${type}</span>`;
  });
};

// --- Event Listeners ---
searchButton.addEventListener('click', getCreatureData);

searchInput.addEventListener('keydown', (e) => {
  if (e.key === 'Enter') {
    getCreatureData();
  }
});

// --- Helper function to simulate a network request ---
function simulateApiCall(query) {
  const mockDatabase = {
    "pyrolynx": { id: 1, name: "Pyrolynx", weight: 42, height: 32, types: ["FIRE"], stats: { hp: 65, attack: 80, defense: 50, "special-attack": 90, "special-defense": 55, speed: 100 }},
    "2": { id: 2, name: "Aquoroc", weight: 220, height: 53, types: ["WATER", "ROCK"], stats: { hp: 85, attack: 90, defense: 120, "special-attack": 60, "special-defense": 70, speed: 40 }},
  };
  // Also allow searching "aquoroc" by name
  mockDatabase["aquoroc"] = mockDatabase["2"];

  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (mockDatabase[query]) {
        resolve(mockDatabase[query]);
      } else {
        reject(new Error("Creature not found"));
      }
    }, 300); // Simulate a 300ms network delay
  });
}

I think there might be a bug here everythings correct from my end.

You likely have a bug in your code, yes.

What are tests 20 and 21?

What specific lines did you write to pass those tests?

How have you tested those lines?

what is this? why are you hardcoding stuff?

yes I’ve tested the lines by using the console function test.

Please answer everything I asked

I don’t know what ‘console function test’ means?

What is this? I don’t see where the instructions ask for this.

If you are using an LLM to write parts of the code, I would stop doing that and remove the code it generated. I would only use the LLM after you understand the code.

Can you just provide the solution as Im not getting anywhere with this? Update your hints page if necessary.

No, we cannot provide you the solution. That is against the rules, especially for the projects.

Note - if you copy answers from anywhere for certification projects, your certification will be revoked, per the academic honesty policy.

The hints page is purposefully empty because this is a certification project, you need to complete it on your own

if you don’t think you have enough knowledge from the practice projects, you may want to do the JS part of the full stack curriculum, it looks like you are missing some pieces of the puzzle