Tell us what’s happening:
Seems like the second last test on this project is not passing.
The test asks for:
When the
#search-input
element contains the value94
and the#search-button
element is clicked, you should add animg
element with theid
ofsprite
and thesrc
set to the Pokémon’sfront_default
sprite to the page.
Which is what my code generates:
<img id="sprite" src="https://raw.githubusercontent.com/PokeAPI/sprites/master/sprites/pokemon/94.png" alt="gengar front default sprite">
Is there a bug in what the test expects or did I get something wrong?
Your code so far
<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div>
<input type="text" id="search-input" placeholder="Search Pokémon" required>
<button id="search-button">Search</button>
</div>
<div id="output">
<h2 id="pokemon-name">Name: </h2>
<p id="pokemon-id">ID: </p>
<p id="weight">Weight: </p>
<p id="height">Height: </p>
<p id="types">Type: </p>
<p id="hp">HP: </p>
<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>
<script src="./script.js"></script>
</body>
</html>
/* file: styles.css */
/* file: script.js */
const searchInput = document.getElementById("search-input");
const searchButton = document.getElementById("search-button");
const outputContainer = document.getElementById("output");
const pokemonNameDisplay = document.getElementById("pokemon-name");
const pokemonIdDisplay = document.getElementById("pokemon-id");
const weightDisplay = document.getElementById("weight");
const heightDisplay = document.getElementById("height");
const typesDisplay = document.getElementById("types");
const hpDisplay = document.getElementById("hp")
const attackDisplay = document.getElementById("attack");
const defenseDisplay = document.getElementById("defense");
const specialAttackDisplay = document.getElementById("special-attack");
const specialDefenceDisplay = document.getElementById("special-defense");
const speedDisplay = document.getElementById("speed");
searchButton.addEventListener("click", () => {
if (!searchInput.value) return;
fetch(`https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/${searchInput.value.toLowerCase()}`)
.then(res => {
// console.log("res:", res, res.ok);
if (!res.ok) throw new Error();
return res.json();
})
.then(data => {
// console.log("data:", data);
displayPokemon(data);
})
.catch(err => {
console.log("err:", err);
alert("Pokémon not found");
});
})
const displayPokemon = (data) => {
pokemonNameDisplay.textContent = data.name.toUpperCase();
pokemonIdDisplay.textContent = data.id;
weightDisplay.textContent = data.weight;
heightDisplay.textContent = data.height;
data.types.forEach((dataType) => {
const button = document.createElement("button");
button.textContent = dataType.type.name.toUpperCase();
typesDisplay.append(button);
});
hpDisplay.textContent = data.stats.find(stat => stat.stat.name === "hp").base_stat;
attackDisplay.textContent = data.stats.find(stat => stat.stat.name === "attack").base_stat;
defenseDisplay.textContent = data.stats.find(stat => stat.stat.name === "defense").base_stat;
specialAttackDisplay.textContent = data.stats.find(stat => stat.stat.name === "special-attack").base_stat;
specialDefenceDisplay.textContent = data.stats.find(stat => stat.stat.name === "special-defense").base_stat;
speedDisplay.textContent = data.stats.find(stat => stat.stat.name === "speed").base_stat;
const imgElem = document.createElement("img");
imgElem.setAttribute("id", "sprite");
imgElem.setAttribute("src", data.sprites.front_default);
imgElem.setAttribute("alt", `${data.name.toLowerCase()} front default sprite`);
outputContainer.appendChild(imgElem);
}
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Challenge Information:
Build a Pokémon Search App Project - Build a Pokémon Search App