Tell us what’s happening:
Im having errors for tests from 15 to 20, the stats arent showing but im only getting the weight, height and the creature’s id
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RPG Creature Finder</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container">
<h1>RPG Creature Finder</h1>
<div class="search-box">
<input type="text" id="search-input" placeholder="Enter creature name or ID" required />
<button id="search-button">Search</button>
</div>
<div class="creature-display">
<div class="image-container">
<img id="creature-image" src="" alt="Creature Image" />
</div>
<div class="info">
<h2 id="creature-name"></h2>
<p>ID: <span id="creature-id"></span></p>
<p><strong>Weight:</strong> <span id="weight"></span></p>
<p><strong>Height:</strong> <span id="height"></span></p>
<div><strong>Type(s):</strong> <span id="types"></span></div>
<div class="stats">
<p><strong>HP:</strong> <span id="hp"></span></p>
<p><strong>Attack:</strong> <span id="attack"></span></p>
<p><strong>Defense:</strong> <span id="defense"></span></p>
<p><strong>Special Attack:</strong> <span id="special-attack"></span></p>
<p><strong>Special Defense:</strong> <span id="special-defense"></span></p>
<p><strong>Speed:</strong> <span id="speed"></span></p>
</div>
</div>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
/* file: styles.css */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f9;
color: #333;
margin: 0;
padding: 0;
}
.container {
max-width: 800px;
margin: 2rem auto;
padding: 1rem;
background: #ffffff;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}
h1 {
text-align: center;
margin-bottom: 1.5rem;
}
.search-box {
display: flex;
justify-content: center;
gap: 1rem;
margin-bottom: 2rem;
}
#search-input {
padding: 0.5rem;
font-size: 1rem;
width: 60%;
}
#search-button {
padding: 0.5rem 1rem;
font-size: 1rem;
background-color: #4caf50;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
}
#search-button:hover {
background-color: #45a049;
}
.creature-display {
display: flex;
gap: 2rem;
align-items: flex-start;
}
.image-container {
flex: 1;
display: flex;
align-items: center;
justify-content: center;
}
#creature-image {
max-width: 100%;
height: auto;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}
.info {
flex: 2;
}
.stats p,
.info p,
#types {
margin: 0.3rem 0;
}
#types span {
display: inline-block;
padding: 0.25rem 0.5rem;
margin-right: 0.3rem;
background-color: #ddd;
border-radius: 5px;
font-weight: bold;
font-size: 0.9rem;
}
/* file: script.js */
document.getElementById("search-button").addEventListener("click", async () => {
const inputRaw = document.getElementById("search-input").value.trim();
const input = isNaN(inputRaw) ? inputRaw.toLowerCase() : inputRaw;
const display = (id, text) => (document.getElementById(id).textContent = text);
const typesEl = document.getElementById("types");
const imageEl = document.getElementById("creature-image");
typesEl.innerHTML = "";
if (!input) return;
try {
const response = await fetch(`https://rpg-creature-api.freecodecamp.rocks/api/creature/${input}`);
if (!response.ok) throw new Error();
const creature = await response.json();
display("creature-name", creature.name?.toUpperCase() || "");
display("creature-id", `#${creature.id}`);
display("weight", creature.weight || "N/A");
display("height", creature.height || "N/A");
// Safely access stats using find
const findStat = (statName) => creature.stats?.find(stat => stat.stat.name === statName)?.base || "N/A";
display("hp", findStat("hp"));
display("attack", findStat("attack"));
display("defense", findStat("defense"));
display("special-attack", findStat("special-attack"));
display("special-defense", findStat("special-defense"));
display("speed", findStat("speed"));
(creature.types || []).forEach((typeObj) => {
const type = typeof typeObj === 'string' ? typeObj : typeObj.type?.name;
if (type) {
const typeEl = document.createElement("span");
typeEl.textContent = type.toUpperCase();
typesEl.appendChild(typeEl);
}
});
imageEl.src = creature.image || "";
imageEl.alt = creature.name || "Creature Image";
} catch (error) {
alert("Creature not found");
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Challenge Information:
Build an RPG Creature Search App Project - Build an RPG Creature Search App