Tell us what’s happening:
Hep me identify the bug that is failing my code. I have been working on it for some time. I am stuck.
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" />
<link rel="stylesheet" href="styles.css" />
<title>Build an RPG Creature Search App</title>
</head>
<body>
<main>
<div id="main-container">
<h1 class="title">Build an RPG Creature Search App</h1>
<div id="search-div">
<label for="search-input"><strong>Search for Crature by Name or ID:</strong></label><br>
<input type="text" name="creature" id="search-input" required />
<button id="search-button">Search</button>
</div>
<div id="creature-info">
<span id="creature-name"></span><span id="creature-id"></span>
<div id="weight"></div>
<div id="height"></div>
</div>
<div id="types"></div>
<div id="base-stats">
<div id="hp"></div>
<div id="attack"></div>
<div id="defense"></div>
<div id="special-attack"></div>
<div id="special-defense"></div>
<div id="speed"></div>
</div>
</div>
<script src="./script.js"></script>
</main>
</body>
</html>
/* file: script.js */
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 types = 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");
const getCreatureData = async () => {
try {
const nameOrId = searchInput.value.toLowerCase();
const res = await fetch(`https://rpg-creature-api.freecodecamp.rocks/api/creature/${nameOrId}`);
const data = await res.json();
//console.log(data);
getCreatureStats(data);
} catch (err) {
alert('Creature not found');
console.log(`Creature not found: ${err}`);
resetAll;
}
};
//getCreatureData();
// ...... destructure the creature data ....
const getCreatureStats = data => {
const {name, id, weight, height, types, stats} = data
creatureName.textContent = `${name.toUpperCase()}`;
creatureID.textContent = `#${id}`;
weight.textContent = `Weight: ${weight}`;
height.textContent = `Height: ${height}`;
hp.textContent = stats[0].base_stat;
attack.textContent = stats[1].base_stat;
defense.textContent = stats[2].base_stat;
specialAttack.textContent = stats[3].base_stat;
specialDefense.textContent = stats[4].base_stat;
speed.textContent = stats[5].base_stat;
types.innerHTML = types.map(obj => `<span>${obj.type.name.toUpperCase()}</span>`).join("");
};
const resetAll = () => {
creatureName.textContent = "";
creatureID.textContent = "";
weight.textContent = "";
height.textContent = "";
hp.textContent = "";
attack.textContent = "";
defense.textContent = "";
specialAttack.textContent = "";
specialDefense.textContent = "";
speed.textContent = "";
types.innerHTML = "";
}
searchButton.addEventListener ("click", e => {
e.preventDefault();
getCreatureData();
resetAll();
});
/* file: styles.css */
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Challenge Information:
Build an RPG Creature Search App Project - Build an RPG Creature Search App