Skip to content

Instantly share code, notes, and snippets.

@jakedohm
Created May 21, 2019 02:11
Show Gist options
  • Save jakedohm/e0e2837bce50815b4e97a3645ee29a1c to your computer and use it in GitHub Desktop.
Save jakedohm/e0e2837bce50815b4e97a3645ee29a1c to your computer and use it in GitHub Desktop.
Showing a refactor of a function with if statements to use key/value pairs in an object
/* Before */
function getErrorMessage(error) {
if(error === 'username') {
return `Username is required`
} else if (error === 'email') {
return `Don't forget your email!`
} else if (error === 'password') {
return `Please enter your password`
}
}
/* After */
function getErrorMessage(error) {
const errorResponses = {
username: `Username is required`,
email: `Don't forget your email!`,
error: `Please enter your password`
}
return errorResponses[error]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment