Skip to content

Instantly share code, notes, and snippets.

@OlivierJM
Last active August 1, 2018 23:20
Show Gist options
  • Save OlivierJM/3ff8c7c1daa8c151cca78c73be5534fd to your computer and use it in GitHub Desktop.
Save OlivierJM/3ff8c7c1daa8c151cca78c73be5534fd to your computer and use it in GitHub Desktop.
// cheap fix
function calculateAge(bornYear, currentYear){
if(bornYear !== undefined && currentYear !== undefined){
console.log(currentYear - bornYear);
} else {
console.log("provide both year values")
}
}
calculateAge(2000, 2018); // 18
calculateAge(); // "provide both year values"
// another cheap fix with one default
function calculateAge2(bornYear, currentYear){
if(arguments.length == 1 && currentYear === undefined){
currentYear = 2018;
console.log(currentYear - bornYear);
} else {
console.log(currentYear - bornYear);
}
}
calculateAge2(2000); // 18
calculateAge2(2010, 2020); // 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment