Last active
September 7, 2021 21:10
-
-
Save santospatrick/4dc8e088ca38329014495f323fc2ba13 to your computer and use it in GitHub Desktop.
Javascript simples immutable data structure exercises
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// =================== | |
// Array Exercises | |
// =================== | |
// Reference: https://pokeapi.co/api/v2/pokemon?limit=10 | |
const arr = [ | |
{ | |
name: 'bulbasaur', | |
url: 'https://pokeapi.co/api/v2/pokemon/1/', | |
base_experience: 64, | |
}, | |
{ | |
name: 'ivysaur', | |
url: 'https://pokeapi.co/api/v2/pokemon/2/', | |
base_experience: 142, | |
}, | |
{ | |
name: 'venusaur', | |
url: 'https://pokeapi.co/api/v2/pokemon/3/', | |
base_experience: 236, | |
}, | |
]; | |
// You can only use "immutable" array methods. | |
// "Immutable" methods keeps the original array form as it is, creating an easier | |
// to manage javascript environment. | |
// You can use: array.map(), array.find(), array.filter(), array.some(), array.reduce() | |
// You CANNOT use: array.forEach(), array.push(), array.pop(), array.splice() | |
// 1. Return only the name of the pokemons | |
// answer: ['bulbasaur', 'ivysaur', 'venusaur'] | |
// 2. Find "venusaur" object in array (and return it) | |
// answer: { "name": "venusaur", "url": "https://pokeapi.co/api/v2/pokemon/3/", "base_experience": 236 } | |
// 3. Return only pokemons that has "base_experience" higher than 100 | |
// answer: [ | |
// { | |
// "name": "ivysaur", | |
// "url": "https://pokeapi.co/api/v2/pokemon/2/", | |
// "base_experience": 142 | |
// }, | |
// { | |
// "name": "venusaur", | |
// "url": "https://pokeapi.co/api/v2/pokemon/3/", | |
// "base_experience": 236 | |
// } | |
// ] | |
// 4. Return "true" if "ivysaur" is in array | |
// answer: true | |
// 5. Return the sum of "base_experience" from all pokemons | |
// answer: 442 | |
// =================== | |
// Object Exercises | |
// =================== | |
// Reference: https://pokeapi.co/api/v2/pokemon/1 | |
const object = { | |
id: 1, | |
name: 'bulbasaur', | |
base_experience: 64, | |
height: 7, | |
weight: 69.5123123, | |
abilities: [ | |
{ | |
name: 'overgrow', | |
url: 'https://pokeapi.co/api/v2/ability/65/' | |
}, | |
{ | |
name: 'chlorophyll', | |
url: 'https://pokeapi.co/api/v2/ability/34/' | |
}, | |
], | |
}; | |
// You can only use "immutable" object methods. | |
// You can use: Object.assign(), Spread Operador, Object Destructuring | |
// You CANNOT use: Object.seal(), "delete" | |
// 1. Return only "base_experience" of the object | |
// answer: 64 | |
// 2. Overwrite "abilities" attribute with an array of strings with the "name" of the each ability and return the whole new object | |
// answer: { ..., abilities: ['overgrow', 'chlorophyll'] } | |
// 3. Create a string describing the object | |
// answer: 'The pokémon "bulbasaur" weighs 69,51 kilograms and he is 7 meters tall. His abilites are: overgrow, chlorophyll' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment