Tell us what’s happening: I need help please, I tried my best but I am keep failing the following are Two errors i have in my codes:
When the
#search-input
element contains the value
Pikachu
and the
#search-button
element is clicked, the values in the
#pokemon-name
,
#pokemon-id
,
#weight
,
#height
,
#hp
,
#attack
,
#defense
,
#special-attack
,
#special-defense
, and
#speed
elements should be
PIKACHU
,
#25
or
25
,
Weight: 60
or
60
,
Height: 4
or
4
,
35
,
55
,
40
,
50
,
50
, and
90
, respectively.
When the
#search-input
element contains the value
94
and the
#search-button
element is clicked, the values in the
#pokemon-name
,
#pokemon-id
,
#weight
,
#height
,
#hp
,
#attack
,
#defense
,
#special-attack
,
#special-defense
, and
#speed
elements should be
GENGAR
,
#94
or
94
,
Weight: 405
or
405
,
Height: 15
or
15
,
60
,
65
,
60
,
130
,
75
, and
110
, respectively.
Your code so far
codes are:
const displayPokemonDetails = (data) => {
const nameElement = document.getElementById(‘pokemon-name’);
const idElement = document.getElementById(‘pokemon-id’);
const weightElement = document.getElementById(‘weight’);
const heightElement = document.getElementById(‘height’);
const hpElement = document.getElementById(‘hp’);
const attackElement = document.getElementById(‘attack’);
const defenseElement = document.getElementById(‘defense’);
const specialAttackElement = document.getElementById(‘special-attack’);
const specialDefenseElement = document.getElementById(‘special-defense’);
const speedElement = document.getElementById(‘speed’);
const spriteElement = document.getElementById(‘sprite’);
const typesElement = document.getElementById(‘types’);
if (!data || data.detail === 'Not Found') {
alert('Pokémon not found');
return;
}
// Clear type element content between searches
typesElement.innerHTML = '';
nameElement.textContent = `Name: ${data.name.charAt(0).toUpperCase() + data.name.slice(1)}`;
idElement.textContent = `ID: #${data.id}`;
weightElement.textContent = `Weight: ${data.weight}`;
heightElement.textContent = `Height: ${data.height}`;
hpElement.textContent = `HP: ${data.stats[0].base_stat}`;
attackElement.textContent = `Attack: ${data.stats[1].base_stat}`;
defenseElement.textContent = `Defense: ${data.stats[2].base_stat}`;
specialAttackElement.textContent = `Special Attack: ${data.stats[3].base_stat}`;
specialDefenseElement.textContent = `Special Defense: ${data.stats[4].base_stat}`;
speedElement.textContent = `Speed: ${data.stats[5].base_stat}`;
// Add types to the typesElement
data.types.forEach(type => {
const typeElement = document.createElement('span');
typeElement.textContent = type.type.name.toUpperCase();
typesElement.appendChild(typeElement);
});
// Set the sprite src attribute
spriteElement.src = data.sprites.front_default;
};
const fetchPokemonData = async (pokemonName) => {
try {
const response = await fetch(https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/${pokemonName}
);
const data = await response.json();
return data;
} catch (error) {
console.error(‘Error fetching Pokémon data:’, error);
return null;
}
};
const handleSearchButtonClick = async () => {
const searchInput = document.getElementById(‘search-input’);
const pokemonName = searchInput.value.toLowerCase();
// Fetch Pokémon data
const data = await fetchPokemonData(pokemonName);
displayPokemonDetails(data);
};
const searchButton = document.getElementById(‘search-button’);
searchButton.addEventListener(‘click’, handleSearchButtonClick);
type or paste code here
WARNING
The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.
You will need to take an additional step here so the code you wrote presents in an easy to read format.
Please copy/paste all the editor code showing in the challenge from where you just linked.
Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.
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