Created
February 20, 2019 05:56
-
-
Save ErikCH/da7060830e7eb1b089860dc325f4207e to your computer and use it in GitHub Desktop.
Alternative To Switch Statements
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
| //Program With Erik | |
| const games = ['legend', 'pokemon', 'fortnite', 'blah'] | |
| games.forEach(val=> { | |
| if(val === 'legend'){ | |
| console.log('legends') | |
| } else if(val === 'pokemon') { | |
| console.log('poke') | |
| } else { | |
| console.log('default') | |
| } | |
| switch(val){ | |
| case 'legend': | |
| console.log('legends') | |
| break; | |
| default: | |
| console.log('test') | |
| break; | |
| } | |
| const getGame= game => { | |
| const itsALegend = _ =>{ | |
| return 'this is a legend' | |
| } | |
| const itsAPokemon = _ => { | |
| return 'this is pokemon' | |
| } | |
| const games = { | |
| legend: itsALegend, | |
| pokemon: itsAPokemon, | |
| default: _=> { | |
| return 'unknown' | |
| } | |
| } | |
| return (games[game] || games.default)() | |
| } | |
| console.log(getGame(val)) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment