Last active
August 1, 2018 23:20
-
-
Save OlivierJM/3ff8c7c1daa8c151cca78c73be5534fd to your computer and use it in GitHub Desktop.
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
// 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