(() => {
// Define the relevant variables
const buttonRefresh = document.querySelector('#refresh-pokedex-list');
const apiRoot = 'https://pokeapi.co/api/v2';
// Define helper functions
const capitalize = string => {
const stringLength = string.length;
// Make the first character of each pokemon uppercase
return (
string.charAt(0).toUpperCase() +
string
.split('')
.splice(1, stringLength - 1)
.join('')
);
};
// Define the relevant functions
getPokemonList = async (limit) => {
const url = apiRoot + '/pokemon?limit=' + limit;
const response = await fetch(url);
const data = await response.json();
return data;
};
createPokemonList = data => {
const pokedexListLength = data.length;
const pokedexApp = document.querySelector('#pokedex-app');
let pokedexList = document.querySelector('.pokedex-list');
// Remove the list from the app, if it exists
if (pokedexList) {
pokedexApp.removeChild(pokedexList);
}
// Create the unordered list element and reassign the pokedexList variable
pokedexList = document.createElement('ul');
pokedexList.classList.add('pokedex-list');
// Now create a list item for each item in the data array
for (let i = 0; i < pokedexListLength; i++) {
// Create the necessary items
const pokedexItem = document.createElement('li');
const pokedexItemName = document.createElement('span');
const pokedexItemIcon = document.createElement('i');
// Capitalize the pokemon's name and get its url
const pokemonName = capitalize(data[i].name);
const pokemonUrl = data[i].url;
// Add the pokemon name and the ID to the created element
pokedexItem.id = pokemonUrl;
pokedexItemName.innerHTML = pokemonName;
// Add the relevant classes
pokedexItem.classList.add('pokedex-list-item');
pokedexItemIcon.classList.add('fas', 'fa-chevron-right');
// Put the items together
pokedexItem.appendChild(pokedexItemName);
pokedexItem.appendChild(pokedexItemIcon);
//...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.