I was playing with Pokemon’s API and I was able to get the names and sprites of the pokemons but got stuck when trying to fetch the array.
async function fetchData()
{
const pokemonName = document.getElementById("pokemonName").value.toLowerCase();
const response = await fetch(`https://pokeapi.co/api/v2/pokemon/${pokemonName}`);
const data = await response.json();
let elementTypes = data.types;
for(let i = 0; i < elementTypes.length; i++)
{
let markup = `<li>${elementTypes[i]}</li>`;
document.getElementById("pokeTypes").innerHTML = markup;
}
}
Pokemon can have multiple types and that’s ‘types’ I want to put it in my DIV, which is ‘pokeTypes’. I can’t seem to throw them into the HTML using a for loop
3
Answers
If i a not wrong you are getting this data right
Need a variable to store each type through the loop. Then set it to the div with id
pokeTypes
. Or it will replace that div content while looping.The JSON response of the Pokémon API typically ends in this way:
So, in order to list all type names of the selected Pokémon you could do something like