Hello can anyone please help or guide me if I am on the correct way with this?
I have trouble getting past step number 15 (Pikachu checkmark), I have tried several solutions and have rewritten my code but nothing seems to work.
I would appreciate any help because this is making me crazy, been stuck with this for hours now…
here is my code so far:
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Pokémon Search App Project</title>
<link rel="stylesheet" href="./styles.css" />
</head>
<body>
<div>
<h1>Pokémon Search App Project</h1>
</div>
<div>
<input type="text" id="search-input" placeholder="Type a Name or ID..." required>
<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>
<p id="types"></p>
<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>
</div>
<script src="./script.js"></script>
</body>
</html>
JS:
// interface
const searchInput = document.getElementById("search-input");
const searchBtn = document.getElementById("search-button");
// pokemon stats
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 specialDefense = document.getElementById("special-defense");
const speed = document.getElementById("speed");
// pokemon stats end
// "https://pokeapi-proxy.freecodecamp.rocks/api/pokemon"
// clear input box
const updateInput = ()=>{
searchInput.value = "";
};
// iterate through types
const updateTypes = (pTypes)=>{
if(pTypes.length > 1){
types.textContent = `Types: `;
for(const type of pTypes){
types.textContent += `${type.type.name.toUpperCase()}, `;
};
types.textContent = types.textContent.slice(0, types.textContent.length-2);
} else{
types.textContent = `Type: `;
for(const type of pTypes){
types.textContent += `${type.type.name.toUpperCase()} `;
};
};
};
// iterate through stats
// update and show stats
const updateStats = (name, id, pWeight, pHeight, pTypes, pStats)=>{
pokemonName.textContent = `Name: ${name.toUpperCase()}`;
pokemonId.textContent = `ID: #${id}`;
weight.textContent = `Weight: ${pWeight}`;
height.textContent = `Height: ${pHeight}`;
updateTypes(pTypes);
hp.textContent = `HP: ${pStats[0].base_stat}`;
attack.textContent = `Attack: ${pStats[1].base_stat}`;
defense.textContent = `Defense: ${pStats[2].base_stat}`;
specialAttack.textContent = `Sp. Attack: ${pStats[3].base_stat}`;
specialDefense.textContent = `Sp. Defense: ${pStats[4].base_stat}`;
speed.textContent = `Speed: ${pStats[5].base_stat}`;
};
// fetch and process data
const fetchData = (input)=>{
fetch(`https://pokeapi-proxy.freecodecamp.rocks/api/pokemon/${input}`)
.then((response)=>{
if(response){
return response.json();
} if(!response) {
alert("Pokémon not found")
}
})
.then((data)=>{
console.log(data);
updateInput();
updateStats(data.name, data.id, data.weight, data.height, data.types, data.stats);
})
.catch((error)=>{
alert("Pokémon not found");
console.log(`ein fehler ist aufgetreten: ${error}`)
})
};
// eventListener for searchBtn
searchBtn.addEventListener("click", ()=>{
console.log(searchInput.value);
fetchData(searchInput.value.toLowerCase());
});