Last active
July 9, 2018 04:37
-
-
Save samanthaming/14b4d7a970b47ce850bd7a5855dc3d66 to your computer and use it in GitHub Desktop.
Code Tidbits: #23 No Else Return (Medium Post)
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
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