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
const pokemon = [ | |
{ | |
name: 'Charmander', | |
type: 'Fire' | |
}, | |
{ name: 'Squirtle', type: 'Water', abilities: ['Torrent', 'Rain Dish'] }, | |
{ | |
name: 'Bulbasur', | |
type: 'Plant' | |
} |
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
const pokemon = { | |
name: 'Squirtle', | |
type: 'Water', | |
abilities: ['Torrent', 'Rain Dish'] | |
}; | |
const squirtleClone = { ...pokemon, abilities: [...pokemon.abilities] }; | |
pokemon.name = 'Charmander'; | |
pokemon.abilities.push('Surf'); |
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
const pokemon = { | |
useless: 'useless property', | |
id: 1, | |
name: 'Squirtle', | |
type: 'Water' | |
}; | |
// Removing the useless property: | |
({ useless, ...newPokemon } = pokemon); | |
console.log(newPokemon); // Result: { id: 1, name: 'Squirtle', type: 'Water' } |
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
const pokemon = { | |
useless: 'Useless property', | |
id: 1, | |
name: 'Squirtle', | |
useless2: 'Another useless prop', | |
type: 'Water' | |
}; | |
// Removing both useless and useless2 properties: | |
({ id, useless, useless2, ...newPokemon } = pokemon); |
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
const pokemon = { | |
useless: 'useless property', | |
id: 1, | |
name: 'Squirtle', | |
type: 'Water' | |
}; | |
const remove = (property, { [property]: value, ...newObject }) => newObject; | |
console.log(remove('useless', pokemon)); // Result: { id: 1, name: 'Squirtle', type: 'Water' } |
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
function getPokemon(type) { | |
let pokemon; | |
switch (type) { | |
case 'Water': | |
pokemon = 'Squirtle'; | |
break; | |
case 'Fire': | |
pokemon = 'Charmander'; | |
break; | |
case 'Plant': |
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
const pokemon = new Map([ | |
['Water', 'Squirtle'], | |
['Fire', 'Charmander'], | |
['Plant', 'Bulbasur'], | |
['Electric', 'Pikachu'] | |
]); | |
function getPokemon(type) { | |
return pokemon.get(type) || 'Mew'; | |
} |
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
function checkGameStatus() { | |
if ( | |
remaining === 0 || | |
(remaining === 1 && remainingPlayers === 1) || | |
remainingPlayers === 0 | |
) { | |
quitGame(); | |
} | |
} |
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
function isGameLost() { | |
return ( | |
remaining === 0 || | |
(remaining === 1 && remainingPlayers === 1) || | |
remainingPlayers === 0 | |
); | |
} | |
// Our function is now much easier to understand: | |
function checkGameStatus() { |
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
function getNews(type) { | |
const allNews = getNewsFromWeb(); | |
return getNewsContent(allNews, type); | |
} | |
function getNewsContent(newsList, type) { | |
const news = []; | |
for (let i = newsList.length - 1; i >= 0; i--) { | |
if (newsList[i].type === type) { | |
news.push(newsList[i].content); |