Hi, I have the same problem every time I try to complete the Pokemon app. The app actually works and gives the desired results, but I still can’t pass the test because I have alawys the two following open points.
-
When the
#search-input
element contains the valuePikachu
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 bePIKACHU
,#25
or25
,Weight: 60
or60
,Height: 4
or4
,35
,55
,40
,50
,50
, and90
, respectively. -
When the
#search-input
element contains the value94
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 beGENGAR
,#94
or94
,Weight: 405
or405
,Height: 15
or15
,60
,65
,60
,130
,75
, and110
, respectively.
Here is my Code:
HTML
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pokémon Search App</title>
<style>
#types div {
display: inline-block;
margin-right: 5px;
}
</style>
</head>
<body>
<div>
<input type="text" id="search-input" required placeholder="Enter Pokémon name or ID">
<button id="search-button">Search</button>
</div>
<div>
<p id="pokemon-name"></p>
<p id="pokemon-id"></p>
<p id="weight"></p>
<p id="height"></p>
<div id="types"></div>
<p id="hp"></p>
<p id="attack"></p>
<p id="defense"></p>
<p id="special-attack"></p>
<p id="special-defense"></p>
<p id="speed"></p>
<img id="sprite" alt="Pokémon sprite">
</div>
<script src="script.js"></script>
</body>
</html>
JS
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-button");
const pokemonName = document.getElementById("pokemon-name");
const pokemonId = document.getElementById("pokemon-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 specialDefence = document.getElementById("special-defense");
const speed = document.getElementById("speed");
const sprite = document.getElementById("sprite");
const pokemonAPI = "https://pokeapi.co/api/v2/pokemon";
searchBtn.addEventListener("click", searchBtnFunc);
function searchBtnFunc() {
const input = searchInput.value.trim().toLowerCase();
if (!input) {
alert("Please type in a Pokémon name or ID");
return;
}
const isNumeric = !isNaN(input);
const query = isNumeric ? input : input.toLowerCase();
fetchPokemonData(query);
}
function fetchPokemonData(query) {
fetch(`${pokemonAPI}/${query}`)
.then(response => {
if (!response.ok) {
throw new Error('Pokémon not found');
}
return response.json();
})
.then(data => {
displayPokemonTraits(data);
searchInput.readOnly = true;
})
.catch(error => {
alert(error.message);
resetFields();
});
}
function displayPokemonTraits(traitData) {
pokemonName.textContent = traitData.name.toUpperCase();
pokemonId.textContent = `#${traitData.id}`;
weight.textContent = `Weight: ${traitData.weight}`;
height.textContent = `Height: ${traitData.height}`;
// Clear previous types
types.innerHTML = '';
// Display Pokémon types
traitData.types.forEach(typeInfo => {
const typeElement = document.createElement('div');
typeElement.textContent = typeInfo.type.name.toUpperCase();
types.appendChild(typeElement);
});
// Display Pokémon stats
hp.textContent = `HP: ${traitData.stats.find(stat => stat.stat.name === 'hp').base_stat}`;
attack.textContent = `Attack: ${traitData.stats.find(stat => stat.stat.name === 'attack').base_stat}`;
defense.textContent = `Defense: ${traitData.stats.find(stat => stat.stat.name === 'defense').base_stat}`;
specialAttack.textContent = `Special Attack: ${traitData.stats.find(stat => stat.stat.name === 'special-attack').base_stat}`;
specialDefence.textContent = `Special Defense: ${traitData.stats.find(stat => stat.stat.name === 'special-defense').base_stat}`;
speed.textContent = `Speed: ${traitData.stats.find(stat => stat.stat.name === 'speed').base_stat}`;
// Display Pokémon sprite
sprite.src = traitData.sprites.front_default;
sprite.alt = `${traitData.name} sprite`;
}
function resetFields() {
searchInput.value = '';
pokemonName.textContent = '';
pokemonId.textContent = '';
weight.textContent = '';
height.textContent = '';
types.innerHTML = ''; // Clear types container
hp.textContent = '';
attack.textContent = '';
defense.textContent = '';
specialAttack.textContent = '';
specialDefence.textContent = '';
speed.textContent = '';
sprite.src = ''; // Clear sprite
sprite.alt = '';
searchInput.readOnly = false;
}
I know I must have missed something. Can someone please help me?