Skip to content

Instantly share code, notes, and snippets.

@samanthaming
Last active July 9, 2018 04:37
Show Gist options
  • Save samanthaming/14b4d7a970b47ce850bd7a5855dc3d66 to your computer and use it in GitHub Desktop.
Save samanthaming/14b4d7a970b47ce850bd7a5855dc3d66 to your computer and use it in GitHub Desktop.
Code Tidbits: #23 No Else Return (Medium Post)
function calcPercentage(number) {
// Guard #1: Stop execution if it's not a valie number
if(typeof number !== 'number') {
return 'Please enter valid whole number';
}
// By this point, you know if will be a valid number
// Guard #2: Stop execution if number is greater than 1
if (number > 1) {
return 'Number must be less than 1';
}
// Finally by this point, you know all the conditions have been met
// Now, it's safe to perform your execution 🎉
return `${number * 100}%`;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment