Created
May 21, 2019 02:11
-
-
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
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
/* 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