benjaminvr

Codecademy - How old?

Jul 7th, 2021 (edited)
227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. 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:
  3.  
  4. If the year is in the future, you should return a string in the following format:
  5. 'You will be [calculated age] in the year [year passed in]'
  6.  
  7. If the year is before they were born, you should return a string in the following format:
  8. 'The year [year passed in] was [calculated number of years] years before you were born'
  9.  
  10. If the year is in the past but not before the person was born, you should return a string in the following format:
  11. 'You were [calculated age] in the year [year passed in]'
  12. */
  13.  
  14. const currentYear = 2021;
  15.  
  16. let howOld = (year, age) => {
  17.   if(year > currentYear){
  18.     return `You will be ${age+(year-currentYear)} in the year ${year}`;
  19.   } else if (year < (currentYear-age)){
  20.     return `The year ${year} was ${(currentYear-age)-year} years before you were born`;
  21.   } else if (year < currentYear){
  22.     return `You were ${age-(currentYear-year)} in the year ${year}`;
  23.   }
  24. }
  25.  
  26.  
  27. console.log(howOld(2030, 20));
  28. console.log(howOld(1960, 20));
  29. console.log(howOld(2018, 20));
  30. /*
  31. You will be 29 in the year 2030
  32. The year 1960 was 41 years before you were born
  33. You were 17 in the year 2018
Add Comment
Please, Sign In to add comment