Hi guys, this is my code:
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 creatureWeight = document.getElementById('weight');
const creatureHeight = document.getElementById('height');
const creatureTypes = document.getElementById('types');
const creatureHp = document.getElementById('hp');
const creatureAttack = document.getElementById('attack');
const creatureDefense = document.getElementById('defense');
const creatureSpecialAttack = document.getElementById('special-attack');
const creatureSpecialDefense = document.getElementById('special-defense');
const creatureSpeed = document.getElementById('speed');
async function getCreature(){
const nameOrId = searchInput.value.toString().toLowerCase().trim();
try {
const data = await fetch(`https://rpg-creature-api.freecodecamp.rocks/api/creature/${nameOrId}`)
const creature = await data.json()
if(creature){
const {name, id, weight, height, types, stats} = creature;
creatureName.textContent = name;
creatureId.textContent = `#${id}`;
creatureWeight.textContent = `Weight: ${weight}`;
creatureHeight.textContent = `Height: ${height}`;
creatureTypes.textContent = `Type: ${types.length > 1? types[0].name + "," + types[1].name: types[0].name }`
creatureHp.textContent = `Hp: ${stats[0].base_stat}`;
creatureAttack.textContent = `Attack: ${stats[1].base_stat}`;
creatureDefense.textContent = `Defense: ${stats[2].base_stat}`;
creatureSpecialAttack.textContent = `Special Attack: ${stats[3].base_stat}`;
creatureSpecialDefense.textContent = `Special Defense: ${stats[4].base_stat}`;
creatureSpeed.textContent = `Speed: ${stats[5].base_stat}`;
}
else {
alert("Creature not found");
}
}
catch {
alert("Creature not found")
}
}
searchButton.addEventListener("click", getCreature)
I dont know why my code doesn’t pass, the code throws a error when the input is incorrect, and fills the elements with the correct data when the input is valid.