So I'm doing this code academy exercise and I realize that its not really working. I think at this point its more about a concept I am missing than about syntax. But I don't see the point of this exercise and so that's why I'm asking.
What is the point of this exercise and why is it I can't get the howOld function to work?
So based on that, here is my code:
What is the point of this exercise and why is it I can't get the howOld function to work?
Quote: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 the person was born, you should return a string in the following format:
'You were [calculated age] in the year [year passed in]'
So based on that, here is my code:
Code:
function howOld(age, year) {
if (year > Date().getFullYear()) {
var ageAtYear = year + (Date().getFullYear() - age);
return `You will be ${age} in the year ${year}`; // Will this calculate my age in the future?
} else if (year > Date().getFullYear()) { // Will this really be the case where I have not been born yet?
var yearsBeforeBorn = age - (Date().getFullYear() - year); // Missing semicolon.
return `The year ${year} was ${yearsBeforeBorn} years before you were born`;
} else {
return `You were ${age} in the year ${year}.`; // Will this calculate my age in the given year? Missing curly brace and semicolon.
}
}