This is my code that I am debugging I’m almost there however I’m experiencing some difficulties.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>RPG Creature Search</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<main id="app">
<h1>RPG Creature Search</h1>
<section id="search-area">
<label for="search-input">Search by name or ID</label>
<input id="search-input" type="text" required placeholder="e.g. Pyrolynx or 2" />
<button id="search-button" type="button">Search</button>
</section>
<section id="result-area">
<h2 id="creature-name">—</h2>
<p id="creature-id">#—</p>
<p id="weight">Weight: —</p>
<p id="height">Height: —</p>
<div id="types" aria-live="polite"></div>
<div id="stats-list">
<p>HP: <span id="hp">—</span></p>
<p>Attack: <span id="attack">—</span></p>
<p>Defense: <span id="defense">—</span></p>
<p>Special Attack: <span id="special-attack">—</span></p>
<p>Special Defense: <span id="special-defense">—</span></p>
<p>Speed: <span id="speed">—</span></p>
</div>
</section>
</main>
<script src="script.js"></script>
</body>
</html>
:root {
--bg: #0a0a23;
--card: #111;
--accent: #ffac33;
--text: #ffffff;
}
html,body {
height: 100%;
margin: 0;
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial;
background: linear-gradient(180deg, var(--bg), #07102b);
color: var(--text);
}
#app {
max-width: 700px;
margin: 28px auto;
background: #0f1724;
padding: 20px;
border-radius: 8px;
box-shadow: 0 6px 24px rgba(0,0,0,0.6);
}
h1 { margin: 0 0 12px 0; font-size: 1.4rem; }
#search-area { display:flex; gap:8px; align-items:center; margin-bottom:16px; }
#search-input { flex:1; padding:8px 10px; border-radius:6px; border:1px solid #26314b; background:#0a1220; color:var(--text); }
#search-button { padding:8px 14px; border-radius:6px; border: none; background:var(--accent); color: #08121a; cursor:pointer; font-weight:600; }
#result-area { background:#071428; padding:12px; border-radius:6px; }
#types { display:flex; gap:6px; flex-wrap:wrap; margin:8px 0 12px 0; }
.type-pill {
background:#223a4a;
color:var(--text);
padding:6px 10px;
border-radius:999px;
font-weight:700;
font-size:0.85rem;
border:1px solid rgba(255,255,255,0.06);
}
#stats-list { display:grid; grid-template-columns: repeat(3, 1fr); gap:8px; margin-top:8px; }
#stats-list p { margin:0; font-size:0.95rem; }
// script.js - FreeCodeCamp RPG Creature Search
// API base
const API_BASE = 'https://rpg-creature-api.freecodecamp.rocks/api';
const CREATURE_ENDPOINT = `${API_BASE}/creature`;
// DOM references
const input = document.getElementById('search-input');
const button = document.getElementById('search-button');
const elName = document.getElementById('creature-name');
const elId = document.getElementById('creature-id');
const elWeight = document.getElementById('weight');
const elHeight = document.getElementById('height');
const elTypes = document.getElementById('types');
const elHP = document.getElementById('hp');
const elAttack = document.getElementById('attack');
const elDefense = document.getElementById('defense');
const elSpAtk = document.getElementById('special-attack');
const elSpDef = document.getElementById('special-defense');
const elSpeed = document.getElementById('speed');
// Reset UI between searches
function clearUI() {
elName.textContent = '—';
elId.textContent = '#—';
elWeight.textContent = 'Weight: —';
elHeight.textContent = 'Height: —';
elTypes.innerHTML = '';
elHP.textContent = '—';
elAttack.textContent = '—';
elDefense.textContent = '—';
elSpAtk.textContent = '—';
elSpDef.textContent = '—';
elSpeed.textContent = '—';
}
// Populate UI with data from API
function populateUI(creature) {
// Uppercase name
elName.textContent = (creature.name || '').toUpperCase();
// ID (# prefix)
elId.textContent = `#${creature.id}`;
// Weight & height
elWeight.textContent = `Weight: ${creature.weight}`;
elHeight.textContent = `Height: ${creature.height}`;
// Types
elTypes.innerHTML = '';
if (Array.isArray(creature.types)) {
creature.types.forEach(type => {
const pill = document.createElement('div');
pill.textContent = type.toUpperCase();
elTypes.appendChild(pill);
});
}
// Stats (nested in creature.stats with dash-case keys)
const stats = creature.stats || {};
elHP.textContent = stats['hp'] ?? '—';
elAttack.textContent = stats['attack'] ?? '—';
elDefense.textContent = stats['defense'] ?? '—';
elSpAtk.textContent = stats['special-attack'] ?? '—';
elSpDef.textContent = stats['special-defense'] ?? '—';
elSpeed.textContent = stats['speed'] ?? '—';
}
// Show "Creature not found"
function showNotFound() {
clearUI();
alert('Creature not found');
}
// Fetch creature by name or ID
async function fetchCreature(term) {
if (!term) {
showNotFound();
return;
}
const url = `${CREATURE_ENDPOINT}/${encodeURIComponent(term.trim())}`;
try {
const res = await fetch(url);
if (!res.ok) {
showNotFound();
return;
}
const data = await res.json();
let creature = data.creature ?? data;
if (!creature || !creature.name) {
showNotFound();
return;
}
populateUI(creature);
} catch (err) {
console.error('Fetch error:', err);
showNotFound();
}
}
// Event listeners
button.addEventListener('click', () => {
fetchCreature(input.value);
});
input.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
button.click();
}
});
// Initialize
clearUI();
here are the tasks I’m stuck on
// running tests
15. When the #search-input element contains the value Pyrolynx and the #search-button element is clicked, the values in the #creature-name, #creature-id, #weight, #height, #hp, #attack, #defense, #special-attack, #special-defense, and #speed elements should be PYROLYNX, #1 or 1, Weight: 42 or 42, Height: 32 or 32, 65, 80, 50, 90, 55, and 100, respectively.
16. When the #search-input element contains the value Pyrolynx and the #search-button element is clicked, a single element should be added within the #types element that contains the text FIRE. The #types element content should be cleared between searches.
17. When the #search-input element contains the value 2 and the #search-button element is clicked, the values in the #creature-name, #creature-id, #weight, #height, #hp, #attack, #defense, #special-attack, #special-defense, and #speed elements should be AQUOROC, #2 or 2, Weight: 220 or 220, Height: 53 or 53, 85, 90, 120, 60, 70, and 40, respectively.
18. When the #search-input element contains the value 2 and the #search-button element is clicked, two elements should be added within the #types element that contain text values WATER and ROCK, respectively. The #types element content should be cleared between searches.
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.
// tests completed