Skip to content

Commit d22c859

Browse files
committed
End Code
1 parent 622ac43 commit d22c859

File tree

9 files changed

+209
-64
lines changed

9 files changed

+209
-64
lines changed

Diff for: end/img/pokeball.jpg

146 KB
Loading

Diff for: end/index.html

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<!-- Tell the browser that it's dealing with HTML5 -->
2+
<!DOCTYPE html>
3+
4+
<!-- Start the HTML document -->
5+
<html lang="en" class="vh-100 overflow-hidden">
6+
<!-- Information for the browser -->
7+
<head>
8+
<!-- Information not visible to the user -->
9+
<meta charset="utf-8">
10+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
11+
12+
<!-- The title of the page, visible in the tab -->
13+
<title>Pokédex</title>
14+
15+
<!-- Bootstrap; I'm lazy -->
16+
<link
17+
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
18+
rel="stylesheet"
19+
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
20+
crossorigin="anonymous">
21+
22+
<!-- Custom CSS -->
23+
<link rel="stylesheet" href="style.css">
24+
</head>
25+
26+
<!-- The stuff the user sees -->
27+
<body class="p-5 h-100">
28+
<!-- The actual Pokédex -->
29+
<div id="pokedex" class="container-fluid rounded h-100 p-4 bg-danger" style="max-width: 1200px; max-height: 1000px;">
30+
<!-- The top bar, containing title and searchbar -->
31+
<div class="row mb-3" id="searchbar">
32+
<div class="col-12">
33+
<h2 class="text-white text-center">Pokédex</h2>
34+
</div>
35+
<div class="col-12 pe-0">
36+
<input type="text" class="form-control" id="search" title="search" placeholder="search..."/>
37+
</div>
38+
</div>
39+
40+
<!-- The main area, containing the image and the selection fields -->
41+
<div class="row h-75">
42+
<div class="col-8 h-100">
43+
<img src="./img/pokeball.jpg" alt="Placeholder image" class="rounded h-100 w-100 bg-white">
44+
</div>
45+
46+
<div class="col-4 h-100 bg-white rounded p-0">
47+
<ul class="list-group overflow-auto h-100" id="pokemon"></ul>
48+
</div>
49+
</div>
50+
51+
<!-- The pokemon data -->
52+
<div class="row h-25 mt-3">
53+
<div class="col-12 text-white">
54+
<h3 id="name-and-number">Select a Pokémon</h3>
55+
<h6 id="description"></h6>
56+
</div>
57+
</div>
58+
</div>
59+
60+
<script src="./js/app.js"></script>
61+
</body>
62+
</html>

Diff for: end/js/app.js

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Prepare a variable to store Pokémon data
2+
let pokedex = [];
3+
let selectedPokemon = '000';
4+
5+
// Wait for the webpage to load and execute the main program
6+
window.addEventListener('load', async function() {
7+
// Load the Pokémon data
8+
pokedex = await loadPokemon();
9+
10+
// Fill the list with all Pokémon
11+
generatePokémonList(pokedex);
12+
13+
// Assign the search function
14+
document.getElementById('search').oninput = search;
15+
});
16+
17+
/**
18+
* This function filters the Pokémon list based on the input of the user inside the search box.
19+
*
20+
* @param {*} event The event that triggered the function.
21+
*/
22+
function search(event) {
23+
// Get the current search term
24+
const term = event.target.value.toLowerCase();
25+
26+
// Filter the Pokémon based on the term
27+
const filteredPokemon = pokedex.filter(pokemon => {
28+
return pokemon.name.toLowerCase().includes(term) || pokemon.number.includes(term)
29+
});
30+
31+
// Generate the new Pokémon list
32+
generatePokémonList(filteredPokemon)
33+
}
34+
35+
/**
36+
* Generate a list of Pokémon for the user to choose from.
37+
* @param {[{name, number, description, category, imageurl, length, weight, abilities, typing}]} pokemon The pokémon to put in the list.
38+
*/
39+
function generatePokémonList(pokemon) {
40+
// Select the list from the HTML DOM
41+
const pokemonList = document.getElementById('pokemon');
42+
43+
// Clear the content of the list
44+
pokemonList.innerHTML = '';
45+
46+
// Loop over all the Pokémon
47+
pokemon.forEach(function(singlePokemon) {
48+
// Add a list item to the list
49+
document.getElementById('pokemon').appendChild(generateListItem(singlePokemon));
50+
});
51+
}
52+
53+
/**
54+
* Create a single list item.
55+
*
56+
* @param {{name, number, description, category, imageurl, length, weight, abilities, typing}} pokemon the pokemon to create a list item for.
57+
*/
58+
function generateListItem(pokemon) {
59+
// Create the list item
60+
const listitem = document.createElement('li');
61+
62+
// Set the attributes
63+
listitem.innerHTML = getPokemonNumberAndName(pokemon)
64+
listitem.className = 'list-group-item list-group-item-action clickable';
65+
66+
// Check whether this Pokémon is selected
67+
if (pokemon.number === selectedPokemon) {
68+
// Add the active class
69+
listitem.className += ' active';
70+
}
71+
72+
// Assign the on-click function
73+
listitem.onclick = selectPokemonFromList;
74+
75+
// Return the list item
76+
return listitem;
77+
}
78+
79+
/**
80+
* Event: When clicked on a single Pokémon, this function loads the data of that Pokémon.
81+
*
82+
* @param {*} event The event that triggered the function.
83+
*/
84+
function selectPokemonFromList(event) {
85+
// Get the Pokémon number & save it
86+
const number = event.target.innerHTML.split(' ')[0];
87+
selectedPokemon = number;
88+
89+
// Check there is an active field
90+
if (document.querySelector('.active')) {
91+
// Remove the active class from that field
92+
document.querySelector('.active').classList.remove('active');
93+
}
94+
95+
// Add the active field to the list item that was clicked
96+
event.target.className += ' active';
97+
98+
// Display the data for the current Pokémon
99+
DisplayPokémonData(pokedex.find(pokemon => pokemon.number === number));
100+
}
101+
102+
/**
103+
* Display the data of a single Pokémon object.
104+
*
105+
* @param {{name, number, description, category, imageurl, length, weight, abilities, typing}} pokemon the pokemon object to display.
106+
*/
107+
function DisplayPokémonData(pokemon) {
108+
// Display all the data on the DOM of the provided Pokémon
109+
document.getElementById('name-and-number').innerHTML = getPokemonNumberAndName(pokemon);
110+
document.getElementById('description').innerHTML = pokemon.description;
111+
document.querySelector('img').src = pokemon.imageurl;
112+
}
113+
114+
/**
115+
* Get the number and the name of a single Pokémon in the following format:
116+
* '{number} {name}'.
117+
*
118+
* @param {{name, number, description, category, imageurl, length, weight, abilities, typing}} pokemon the pokemon to get the data from.
119+
* @returns The formatted string.
120+
*/
121+
function getPokemonNumberAndName(pokemon) {
122+
return `${pokemon.number} ${pokemon.name}`;
123+
}
124+
125+
/**
126+
* Load all the data on all Pokémon available in ./js/pokemon.json.
127+
* @returns {Promise<[{name, number, description, category, imageurl, length, weight, abilities, typing}]>} An array of Pokémon data available.
128+
*/
129+
async function loadPokemon() {
130+
return await fetch('./js/pokemon.json')
131+
.then(response => response.json());
132+
}

Diff for: end/js/pokemon.json

+1
Large diffs are not rendered by default.

Diff for: end/style.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.clickable {
2+
cursor: pointer;
3+
}

Diff for: start/.hintrc

-5
This file was deleted.

Diff for: start/img/pokeball.jpg

146 KB
Loading

Diff for: start/index.html

+9-57
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
rel="stylesheet"
1919
integrity="sha384-rbsA2VBKQhggwzxH7pPCaAqO46MgnOM80zW1RWuH61DGLwZJEdK2Kadq2F9CUG65"
2020
crossorigin="anonymous">
21+
22+
<!-- Custom CSS -->
23+
<link rel="stylesheet" href="style.css">
2124
</head>
2225

2326
<!-- The stuff the user sees -->
@@ -29,78 +32,27 @@
2932
<div class="col-12">
3033
<h2 class="text-white text-center">Pokédex</h2>
3134
</div>
32-
<div class="col-12">
35+
<div class="col-12 pe-0">
3336
<input type="text" class="form-control" id="search" title="search" placeholder="search..."/>
3437
</div>
3538
</div>
3639

3740
<!-- The main area, containing the image and the selection fields -->
3841
<div class="row h-75">
3942
<div class="col-8 h-100">
40-
<img src="https://via.placeholder.com/500" alt="Placeholder image" class="rounded h-100 w-100">
43+
<img src="./img/pokeball.jpg" alt="Placeholder image" class="rounded h-100 w-100 bg-white">
4144
</div>
4245

43-
<div id="pokemon" class="col-4 h-100">
44-
<ul class="list-group overflow-auto rounded h-100">
45-
<li class="list-group-item list-group-item-action">1</li>
46-
<li class="list-group-item list-group-item-action">2</li>
47-
<li class="list-group-item list-group-item-action">3</li>
48-
<li class="list-group-item list-group-item-action">4</li>
49-
<li class="list-group-item list-group-item-action">5</li>
50-
<li class="list-group-item list-group-item-action">6</li>
51-
<li class="list-group-item list-group-item-action">7</li>
52-
<li class="list-group-item list-group-item-action">8</li>
53-
<li class="list-group-item list-group-item-action">9</li>
54-
<li class="list-group-item list-group-item-action">10</li>
55-
<li class="list-group-item list-group-item-action">11</li>
56-
<li class="list-group-item list-group-item-action">12</li>
57-
<li class="list-group-item list-group-item-action">13</li>
58-
<li class="list-group-item list-group-item-action">14</li>
59-
<li class="list-group-item list-group-item-action">15</li>
60-
<li class="list-group-item list-group-item-action">16</li>
61-
<li class="list-group-item list-group-item-action">17</li>
62-
<li class="list-group-item list-group-item-action">18</li>
63-
<li class="list-group-item list-group-item-action">19</li>
64-
<li class="list-group-item list-group-item-action">20</li>
65-
<li class="list-group-item list-group-item-action">21</li>
66-
<li class="list-group-item list-group-item-action">22</li>
67-
<li class="list-group-item list-group-item-action">23</li>
68-
<li class="list-group-item list-group-item-action">24</li>
69-
<li class="list-group-item list-group-item-action">25</li>
70-
<li class="list-group-item list-group-item-action">26</li>
71-
<li class="list-group-item list-group-item-action">27</li>
72-
<li class="list-group-item list-group-item-action">28</li>
73-
<li class="list-group-item list-group-item-action">29</li>
74-
<li class="list-group-item list-group-item-action">30</li>
75-
<li class="list-group-item list-group-item-action">31</li>
76-
<li class="list-group-item list-group-item-action">32</li>
77-
<li class="list-group-item list-group-item-action">33</li>
78-
<li class="list-group-item list-group-item-action">34</li>
79-
<li class="list-group-item list-group-item-action">35</li>
80-
<li class="list-group-item list-group-item-action">36</li>
81-
<li class="list-group-item list-group-item-action">37</li>
82-
<li class="list-group-item list-group-item-action">38</li>
83-
<li class="list-group-item list-group-item-action">39</li>
84-
<li class="list-group-item list-group-item-action">40</li>
85-
<li class="list-group-item list-group-item-action">41</li>
86-
<li class="list-group-item list-group-item-action">42</li>
87-
<li class="list-group-item list-group-item-action">43</li>
88-
<li class="list-group-item list-group-item-action">44</li>
89-
<li class="list-group-item list-group-item-action">45</li>
90-
<li class="list-group-item list-group-item-action">46</li>
91-
<li class="list-group-item list-group-item-action">47</li>
92-
<li class="list-group-item list-group-item-action">48</li>
93-
<li class="list-group-item list-group-item-action">49</li>
94-
<li class="list-group-item list-group-item-action">50</li>
95-
</ul>
46+
<div class="col-4 h-100 bg-white rounded p-0">
47+
<ul class="list-group overflow-auto h-100" id="pokemon"></ul>
9648
</div>
9749
</div>
9850

9951
<!-- The pokemon data -->
10052
<div class="row h-25 mt-3">
10153
<div class="col-12 text-white">
102-
<h3 id="name-and-number">#001 Pokemon</h3>
103-
<p id="description">Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tempore voluptate sapiente deleniti quod illum aspernatur esse sed iure et iste, velit quisquam perspiciatis beatae sequi qui corporis consectetur cum harum fugit! Non reiciendis libero cum molestias exercitationem quos fuga labore.</p>
54+
<h3 id="name-and-number">Select a Pokémon</h3>
55+
<h6 id="description"></h6>
10456
</div>
10557
</div>
10658
</div>

Diff for: start/style.css

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
#pokedex {
2-
background-color: darkred;
1+
.clickable {
2+
cursor: pointer;
33
}

0 commit comments

Comments
 (0)