Tell us what’s happening:
I when typing Red in to search index should get error message as per the story however that seems to not be working all prior steps have passed except step 14 is there something incorrect about the if statement?
Your code so far
<!-- file: index.html -->
<input type="text" id="search-input" required>
<button id="search-button">Search</button>
<div>
<p>Pokémon Name: <span id="pokemon-name"></span></p>
<p>Pokémon ID: <span id="pokemon-id"></span></p>
<p>Weight: <span id="weight"></span></p>
<p>Height: <span id="height"></span></p>
<p>Types: <span id="types"></span></p>
<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>
<div id="pokemon-image"></div>
/* file: styles.css */
/* file: script.js */
document.getElementById('search-button').addEventListener('click', () => {
const searchInput = document.getElementById('search-input').value;
if (searchInput === 'Red') { //
alert('Pokémon not found');
return;
}
fetch(`https://pokeapi.freecodecamp.dev/api/v1/pokemon/${searchInput.toLowerCase()}`)
.then(response => {
if (!response.ok) {
throw new Error('Pokemon not found');
}
return response.json();
})
.then(data => {
document.getElementById('pokemon-name').textContent = data.name.toUpperCase();
document.getElementById('pokemon-id').textContent = `#${data.id}`;
document.getElementById('weight').textContent = `Weight: ${data.weight}`;
document.getElementById('height').textContent = `Height: ${data.height}`;
document.getElementById('hp').textContent = data.hp;
document.getElementById('attack').textContent = data.attack;
document.getElementById('defense').textContent = data.defense;
document.getElementById('special-attack').textContent = data['special-attack'];
document.getElementById('special-defense').textContent = data['special-defense'];
document.getElementById('speed').textContent = data.speed;
document.getElementById('types').innerHTML = '';
data.types.forEach(type => {
const typeElement = document.createElement('span');
typeElement.textContent = type.toUpperCase();
document.getElementById('types').appendChild(typeElement);
});
const spriteElement = document.createElement('img');
spriteElement.id = 'sprite';
spriteElement.src = data.sprites.front_default;
document.getElementById('pokemon-image').innerHTML = '';
document.getElementById('pokemon-image').appendChild(spriteElement);
})
.catch(error => {
alert(error.message);
});
});
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Challenge Information:
Build a Pokémon Search App Project - Build a Pokémon Search App