Tell us what’s happening:
I am stuck on 14. of this project: When the #search-input element contains the value Red and the #search-button element is clicked, an alert should appear with the text “Creature not found”
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<title>Creature Search App</title>
</head>
<body>
<div>
<input id="search-input" required="input">
<button id="search-button">Search</button>
</div>
<div class="overview"> overview
<h3 id="creature-name"></h3>
<h3 id="creature-id"></h3>
<p id="weight">weight</p>
<p id="height">height</p>
<p id="types">types</p>
<p id="hp">hp</p>
</div>
<table>
<div class="stats"> Stats
<p id="attack">attack</p>
<p id="defense">defense</p>
<p id="special-attack">special-attack</p>
<p id="special-defense">special-defense</p>
<p id="speed">speed</p>
</div>
</table>
<script src="/script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
const searchInput = document.getElementById("search-input");
const searchBtn = 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 getCreature = async () => {
try {
const creatureNameorId = searchInput.value.toLowerCase();
const res = await fetch(`https://rpg-creature-api.freecodecamp.rocks/api/creature/${creatureNameorId}`);
const data = await res.json();
setCreatureInfo(data);
} catch (err) {
alert("Creature not found");
console.error(err);
}
};
const setCreatureInfo = data => {
const {creatureName,creatureId,weight,height,hp,attack,defense,specialAttack,specialDefense,speed} = data;
creatureName.textContent = `${creatureName[0].toUpperCase() + creatureName.splice(1)}`
creatureId.textContent = `#${id}`;
weight.textContent = `weight: ${weight}`;
height.textContent = `height: ${height}`;
hp.textContent = data.stats.hp;
attack.textContent = data.stats.attack;
defense.textContent = data.stats.defense;
specialAttack.textContent = data.stats.specialAttack;
specialDefense.textContent = data.stats.specialDefense;
speed.textContent = data.stats.speed;
creatureTypes.innerHTML = types.map(obj => `<span>${obj.type.name[0].toUpperCase() + obj.type.name.slice(1)}</span>`).join(" ");
};
searchBtn.addEventListener("click", e => {
e.preventDefault();
getCreature();
});
searchInput.addEventListener("keydown", e => {
if(e.key === "Enter") {
searchBtn.click();
}
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:142.0) Gecko/20100101 Firefox/142.0
Challenge Information:
Build an RPG Creature Search App Project - Build an RPG Creature Search App