FAQ: Code Challenges: JavaScript Fundamentals - howOld()

ha! wow, that fixed it xD I guess I’m living in the past.

1 Like

My next guess was going to be the missing full stops, but it looks like they are not expected.

1 Like

This one was ■■■■■■ hard to figure out for me!

This one gave me a bit of a headache trying to figure out the necessary equations. I feel pretty proud of my code, as I was able to write it out and run it on the first try - considering with how tricky the logic can be with this one, I surprised myself. Thought I’d share my code with some comments in case it might help anyone out, or if anyone notices another way to streamline it more.

const howOld = (age, year) => {
  const currentYear = new Date().getFullYear(); // get current year
  let ageDifference = year - currentYear; // dealing with differences
  let calculatedAge = Math.abs(ageDifference + age); /* This is offsetting 
the given negative value from when the year given is before the year born 
for the second case */
  if (year > currentYear) { //easy - future years
    return `You will be ${calculatedAge} in the year ${year}`;
  } else if (Math.abs(ageDifference) > age) { /* this is making sure the 
ageDifference value is a positive integer for comparitive purposes */
    return `The year ${year} was ${calculatedAge} years before you were born`;
  } else { //only one case left, no need to explicitly specify
    return `You were ${calculatedAge} in the year ${year}`;

I’m so sad that my code wasn’t aproved, but it works! it gives you exactly what we need to know. I will try other way now, but before I deleted everything, I just wanted to share and ask what you think of my code :stuck_out_tongue:

function howOld (age, year){
  const actualYear = 2021;
  if (year > actualYear){
     newAge = (age + year) - actualYear
    return 'You will be ' +newAge+' in the year '+year
  }else if (year < actualYear){
    newAge = (age - actualYear)+year
    return 'You were '+newAge+ ' in the year '+year+' passed in'
  } else {
    newAge = (age - actualYear)+year
    return 'The year '+year+' was' +newAge+' years before you were born'

console.log(howOld(40, 1960))
//It prints: You were -21 in the year 1960 passed in

Here`s what I tried

const howOld = (age , year) => {

const today = new Date();

const CurrentYear = today.getFullYear();

const YearBorn = CurrentYear - age;


if (year > CurrentYear ){

age = age + (year - CurrentYear);

return You will be ${age} in the year ${year}.

}else if (year < YearBorn ){

age = YearBorn - year;

return The year ${year} was ${age} years before you were born.;

}else if (year > YearBorn && year < CurrentYear ){

age = year - YearBorn;

return You were ${age} in the year ${year}.;






I had to search for this “new Date();” & “today.getFullYear();” as I entered date manually before."

If you have any comments, I would be glade as this is my 1st time coding

This is what I used.

const howOld = (age, year) => {

  if ( year > 2021) {

    let yearDifference = year - 2021;

    return `You will be ${age + yearDifference} in the year ${year}`;

  } else if (year < 2021) {

    let yearDifference = 2021 - year;

    if (age < yearDifference) {

      return `The year ${year} was ${yearDifference - age} years before you were born`;

    } else {

      return `You were ${age - yearDifference} in the year ${year}`;




This is what I used. I added one option more if the year passed is equals to the current year :grinning_face_with_smiling_eyes:

I am so confused as to why this is not working,

function howOld(age,year){
  const theCurrentYear = 2021;
  const yearDifference = year - theCurrentYear;
  const newAge = age + yearDifference;

  if (newAge < 0 ){
    return `The year ${year} was ${newAge} years before you were born`;
  } else if( newAge > age){
    return `You will be ${newAge} in the year ${year}`;
  } else {
    return `You were ${newAge} in the year ${year}`;



Welcome fellow learner xD,

from what i can see, passing in 1995 to your code will create a negative “year difference” so you may be receiving a double negative somewhere in your code which although unintended will add when you intend to subtract, or subtract when you intend to add.

Take what i say with a grain of salt because I am new and still learning, but I would try to reorder the variable (yearDifference) to = theCurrentYear - year.

The instructions are not clear. What exactly asking us to do by using words like “currently and was” in one sentence. If its “currently” they should be using “is”, if its “was” they should be using in the “past”.Or at least usage of a comma would make it easier.I don’t know what I am asked to code here in this exercise because of not clear instructions, and Codecademy has this problem once in a while by not writing clear instructions.