Skip to content

Instantly share code, notes, and snippets.

@ErikCH
Created February 20, 2019 05:56
Show Gist options
  • Select an option

  • Save ErikCH/da7060830e7eb1b089860dc325f4207e to your computer and use it in GitHub Desktop.

Select an option

Save ErikCH/da7060830e7eb1b089860dc325f4207e to your computer and use it in GitHub Desktop.
Alternative To Switch Statements
//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