howOld() "function should return" error message

// Write your function here:

const howOld = (age , year) =>{
  //If yearDifference is positive, then the year is in the future. If yearDifference is negative, the year was in the past.
 //ageInYear represents the increase or decrease in age, associated with the user-entered year.
  const yearDifference = year - 2000
  const ageInYear = age + yearDifference
  if (yearDifference > 0){
      return `You will be ${ageInYear} in the year ${year}`
  } else if (ageInYear < 0){
      return `The year ${year} was ${-ageInYear} years before you were born`
    } else {
      return `You were ${ageInYear} in the year ${year}`
    }
  }

console.log(howOld(10 , 2010))
// Once your function is written, write function calls to test your code!

I am getting the following error message: If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’

I have been tinkering with this script and making it more concise for the last hour. For the life of me I cannot figure out why this error message is still popping up. I even looked at the solution to compare my code with it and I am lost. I would greatly appreciate help.

Hey louismedina449869610
You should calculate the future age.

Let’s say:

age = 18;
year = 2022;
currentYear=2021;

if year - currentYear is positive it means we are talking about the future, so we add the difference to age.
ageInYear will be 19.

If not positive (negative or zero) it means we are not in the future, so we must determine if the difference is greater than our age.

We do -(year - currentYear) to make this quantity positive (this is caveman style, if you want to be sophisticated you could use Math.abs() :wink: )

So if it is greater than our age, then we weren’t born. Else, we were younger.

Does this help?

I’ll also leave you my code about this exercise I did it about an one hour ago.

function howOld(age, year){
  const currentYear = 2021;
  const yearDiff = year - currentYear; //negative if older years, positive for newer years
  if(yearDiff<=0){
    return -yearDiff>age? `The year ${year} was ${-(yearDiff+age)} years before you were born` : `You were ${yearDiff+age} in the year ${year}`
  }else
    return `You will be ${yearDiff+age} in the year ${year}`
};

So it turns out my code was correct. The only issue is that if you use a value for the current year directly(instead of with new Date(), you must use 2021 as your value.

if your code was correct it would not have given any error

Evidently not, since I did not alter the functionality or syntax of my code whatsoever.

So it turns out my code was correct. The only issue is that if you use a value for the current year directly(instead of with new Date(), you must use 2021 as your value.

Evidently not, since I did not alter the functionality or syntax of my code whatsoever.

Sure. however, my code works even not using 2021 as value for the currentYear. lol

You’re welcome by the way.

Cheers,

1 Like