This lab includes another testing library called as Jasmine. Try self-learning. Completing this lab gives you a special badge for this week.
We have just learned some super useful methods, that will help us a lot to manipulate objects and arrays. In this exercise, we will focus on practicing them, and this being said, it is mandatory to implement at least one of them in each iteration.
Best way to practice is to work with real data. In the src/data.js
file you will find an array of info about all the pokemons in your pokedex
-
Fork this repo
-
Clone this repo
-
Practice JavaScript advanced methods (
map
,forEach
,reduce
,filter
andsort
to manipulate arrays)
- Upon completion, run the following commands:
git add .
git commit -m "done"
git push origin master
- Don't create a Pull Request. This is only for practice. Just make sure Jasmine is happy :)
The src/data.js
contains an array of pokemons. Here is one example of how the data is displayed:
{
"id": 1,
"num": "001",
"name": "Bulbasaur",
"img": "http://www.serebii.net/pokemongo/pokemon/001.png",
"type": [
"Grass",
"Poison"
],
"height": "0.71 m",
"weight": "6.9 kg",
"candy": "Bulbasaur Candy",
"candy_count": 25,
"egg": "2 km",
"spawn_chance": 0.69,
"avg_spawns": 69,
"spawn_time": "20:00",
"multipliers": [
1.58
],
"weaknesses": [
"Fire",
"Ice",
"Flying",
"Psychic"
],
"next_evolution": [
{
"num": "002",
"name": "Ivysaur"
},
{
"num": "003",
"name": "Venusaur"
}
]
},
In the next a couple of iterations, you will be using your JS knowledge to manipulate with this data.
Ohh yes! We have our beloved tests, but we have used Jasmine in this lab and not Jest. You don't have to install or run anything using npm
. Open the SpecRunner.html
file on your browser with Live Server and voila, that's it, your tests should show up and now you can start coding to pass the tests. Remember to focus on one test at a time and read carefully the instructions to understand what you have to do. The tests can be found in the tests/pokemons.spec.js
file.
You have to work on the src/pokemons.js
file.
We need to get the array of all Fire type pokemons. Go ahead and create a function named getAllFirePokemons()
that receives an array of pokemons as an argument and returns a new filtered array.
"type": [
"Fire",
],
All fire type pokemons will have that type Fire
Go ahead and create a shortestPokemon()
function that receives an array as a parameter and returns the name
of the shortest pokemon
Create a candyAverage()
function that receives an array as a parameter. This function must return the average of candy_count
for all the pokemons
The average must be returned rounded to 2 decimals! Note: Some pokemons do not have a candy_count
. Handle that scenario gracefully.
💡 Maybe you want to "reduce" the data to a single value. 😉
Create a function getGroundPokeImg()
which accepts an array of pokemons as a parameter and returns an array of images for the first 10 Ground
type pokemons.
Note: Ground
type pokemons have this property in the object
"type": [
"Ground",
],
We might need to use more than a single loop here
Create a function getHeavyPokemons()
that receives an array as parameter and returns a new filtered array with the names of all Pokemons heavier than Pikachu.
Note: All pokemons have a weight
attribute. You moght need to use that
💡 Make sure not to mutate the original array 😉
Create a orderAlphabetically()
function, that receives an array and returns an array of first 20 pokemons, alphabetically ordered based on their name
. Return only the name of each pokemon, and if the array you receive has less than 20 pokemons, return all of them.
💡 Make sure not to mutate the original array 😉
Create a strongPokemons()
function, that receives an array and returns an array of first 15 pokemon names, that have just one weakness
Happy coding! ❤️