Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Write a function, howOld(), that has two number parameters, age and year, and returns how old someone who is currently that age was (or will be) during that year. Handle three different cases:
- If the year is in the future, you should return a string in the following format:
- 'You will be [calculated age] in the year [year passed in]'
- If the year is before they were born, you should return a string in the following format:
- 'The year [year passed in] was [calculated number of years] years before you were born'
- If the year is in the past but not before the person was born, you should return a string in the following format:
- 'You were [calculated age] in the year [year passed in]'
- */
- const currentYear = 2021;
- let howOld = (year, age) => {
- if(year > currentYear){
- return `You will be ${age+(year-currentYear)} in the year ${year}`;
- } else if (year < (currentYear-age)){
- return `The year ${year} was ${(currentYear-age)-year} years before you were born`;
- } else if (year < currentYear){
- return `You were ${age-(currentYear-year)} in the year ${year}`;
- }
- }
- console.log(howOld(2030, 20));
- console.log(howOld(1960, 20));
- console.log(howOld(2018, 20));
- /*
- You will be 29 in the year 2030
- The year 1960 was 41 years before you were born
- You were 17 in the year 2018
Add Comment
Please, Sign In to add comment