Last active
October 8, 2019 17:55
-
-
Save NyaGarcia/fd8f782a1ad85322f9c9a20f771e93c2 to your computer and use it in GitHub Desktop.
Using a Map to replace a switch statement
This file contains hidden or 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'; | |
} | |
console.log(getPokemon('Fire')); // Result: Charmander | |
console.log(getPokemon('unknown')); // Result: Mew |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment