howOld with switch - JavaScript Fundamentals

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}`
    }
}`

This is the key sentence, I suppose. Your function does not return anything but logs the answer straight to the console.
Try to do it the other way round, return from the function and log the function call. That should lead to the same result, but it would be a better practice and maybe lead to your answer being accepted. Often when you write a program, a single function will be just one step to the final result. Another function will work with the returned result of another one.

Thank you very much for the answer! I changed it like you said and it didn’t accept my answer, but then i added a minutes in the ${-newAge} (note the minus in front) and it was all successful :slight_smile:
Thank you again! :slight_smile:
Here is my code now:

const howOld = (age, year) => {
  const currentYear = 2022;
  const yearBorn = currentYear - age;
  const newAge = year - yearBorn;

  switch (true) {
    case year > currentYear:
      return `You will be ${newAge} in the year ${year}`;
      break;
    case year < yearBorn:
      return `The year ${year} was ${-newAge} years before you were born`;
      break;
    case year > yearBorn < currentYear:
      return `You were ${newAge} in the year ${year}`;
      break;
    case (year = currentYear):
      return `You are ${age} years old`;
      break;
    default:
      return "Hey";
  }
};
console.log(howOld(10, 2025));
console.log(howOld(10, 1979));
console.log(howOld(10, 2000));
console.log(howOld(10, 2022));
1 Like