Do API calls with Javascript - Done

This fiddle is the finished project for my blogpost over at https://q-bit.me/. I hope you enjoyed and learned something new from it

by Tobias Quante

HTML

<!DOCTYPE html>
<html lang="en">

  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Learn to make API calls - My Pokedex</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" />
    <link rel="stylesheet" href="styles.css" />
  </head>

  <body>
    <div class="container">
      <div class="toolbar">
        <div class="toolbar-left">
          <h1 class="toolbar-title">My Pokedex</h1>
        </div>
        <div class="toolbar-right">
          <button id="refresh-pokedex-list"><i class="fas fa-redo-alt"></i> Refresh</button>
        </div>
      </div>
      <div id="pokedex-app"></div>
    </div>
    <script src="main.js"></script>
  </body>

</html>

CSS

* {
  font-family: 'Lucida Sans', 'Lucida Sans Regular', 'Lucida Grande', 'Lucida Sans Unicode', Geneva, Verdana, sans-serif;
}

html,
body,
body,
ul,
h1,
p {
  margin: 0;
  padding: 0;
}

body {
  background-color: #eaece5;
}

button {
  transition: background-color 0.25s;
  display: inline-block;
  background-color: #d42b2b;
  color: #fff;
  border: none;
  outline-style: none;
  padding: 0.5rem 1rem;
  border-radius: 0.25rem;
  font-weight: 500;
  font-size: 1rem;
}

button:hover {
  transition: background-color 0.25s;
  background-color: #d84141;
  cursor: pointer;
}

#pokedex-app {
  background-color: #f2f2f2;
  margin-top: 1rem;
}

.container {
  min-height: 10rem;
  border-radius: 0.25rem;
  margin: 1rem auto;
  display: block;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  min-height: 2rem;
  width: 100%;
}

.toolbar-title {
  color: #d84141;
  font-size: 2rem;
}

.pokedex-list {
  list-style: none;
  margin: 0.5rem 1rem;
}

.pokedex-list-item {
  font-size: 1.1rem;
  padding: 0.5rem 1rem;
  border-bottom: 2px solid #eaece5;
  display: flex;
  justify-content: space-between;
}

@media (max-width: 550px) {
  .container {
    width: 90vw;
  }
}

@media (min-width: 550px) {
  .container {
    width: 80vw;
  }
}

@media (min-width: 700px) {
  .container {
    width: 60vw;
  }
}

@media (min-width: 900px) {
  .container {
    width: 50vw;
  }
}

@media (min-width: 1200px) {
  .container {
    width: 40vw;
  }
}

JavaScript

(() => {
	// 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);

			//...