I am getting the results i need to have, but the system still doesn’t accept my answer. Perhaps i’m missing something obvious. This is my code:
const howOld = (age, year) => {
const currentYear = 2022
const yearBorn = currentYear - age;
const newAge = year - yearBorn;
switch (true) {
case (year > currentYear):
console.log(`You will be ${newAge} in the year ${year}`);
break;
case (year < yearBorn):
console.log(`The year ${year} was ${newAge} years before you were born`);
break;
case (year > yearBorn < currentYear):
console.log(`You were ${newAge} in the year ${year}`);
break;
case (year = currentYear):
console.log(`You are ${age} years old`);
break;
default:
console.log("Hey")
}
}
howOld(10, 2025);
howOld(10, 1979);
howOld(10, 2000);
howOld(10, 2022);
And my output is:
You will be 13 in the year 2025
The year 1979 was -33 years before you were born
The year 2000 was -12 years before you were born
You were 10 in the year 2022
When i RUN it, the popup says :
“If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’”
What am i missing?
This is a link to the exercise:
https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/how-old
The system gives me the following code as a recommended solution:
`const howOld = (age, year) => {
// The following two lines make it so that our function always knows the current year.
let dateToday = new Date();
let thisYear = dateToday.getFullYear();
// It is totally ok if your function used the current year directly!
const yearDifference = year - thisYear
const newAge = age + yearDifference
if (newAge > age) {
return `You will be ${newAge} in the year ${year}`
} else if (newAge < 0) {
return `The year ${year} was ${-newAge} years before you were born`
} else {
return `You were ${newAge} in the year ${year}`
}
}`