Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save samanthaming/ef64e8af15791ae02f5a95d4241ca7f6 to your computer and use it in GitHub Desktop.
Save samanthaming/ef64e8af15791ae02f5a95d4241ca7f6 to your computer and use it in GitHub Desktop.
Code Tidbits: #23 No Else Return (Medium Post)
// ❌ You can skip the else block
function calcPercentage(number) {
// Check if valid number
if(typeof number === 'number') {
// Another check: only do calculation number is less than 1
if (number > 1) {
return 'Number must be less than 1';
} else {
return `${number * 100}%`;
}
} else {
return 'Please enter valid whole number';
}
}
// ✅ Yay, much easier to read
function calcPercentage(number) {
if(typeof number === 'number') {
if (number > 1) {
return 'Number must be less than 1';
}
return `${number * 100}%`;
}
return 'Please enter valid whole number';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment