FAQ: Code Challenges: JavaScript Fundamentals - howOld()

You can also use this return:


return `The year ${year} was ${newAge*-1} years before you were born`;

Hi everyone!

I would really appreciate it if someone could help me out with the following matter:

I think my code is right–when I test out each of the three scenarios the challenge instructions asked me to address, it seems to be working. However, I keep getting a message that I’m not addressing one of the guidelines (even though my code seems to address this fine):

Screenshot 2022-03-09 230450

I’d really appreciate any insights!

Here is my code:


const howOld = (age, year) => {

let ddate = new Date(); 
let currentYear = ddate.getFullYear(); 
let yearBorn = currentYear - age; 

if (year > currentYear) {
  let futureAge = year - yearBorn; 
  return `You will be ${futureAge} in the year ${year}`; 
} 
else if (year < yearBorn) {
  let yearGap = yearBorn - year; 
  return `The year ${year} was ${yearGap} years before you were born`; 
}
else if (year < currentYear && year > yearBorn) {
  let pastAge = year - yearBorn; 
  return `You were ${pastAge} in the year ${year}`; 
}
}

Thanks again!

Also, I forgot to mention that these are some of the results I got:

console.log (howOld(20, 2043)); //prints: You will be 41 in the year 2043
console.log (howOld(20, 2005)); //prints: You were 3 in the year 2005
console.log (howOld(20, 1995)); //prints: The year 1995 was 7 years before you were born

Thanks!

I did it this way:

const howOld = (age, year) => {
let currentYear=2022;
let yearOfBirth=currentYear - age;
if (currentYear < year){return You will be ${year - yearOfBirth} in the year ${year}}; //future
if (yearOfBirth > year) {return The year ${year} was ${yearOfBirth - year} years before you were born};
if (currentYear > year) {age = year-yearOfBirth}; return You were ${age} in the year ${year};
}

I didn’t know about the time function, it’s a shame it wasn’t mentioned in the study material.

// Write your function here:
function howOld(age, year) {
var message;
var BirthYear, Calc;
var todayYear = Date().substr(11, 4);
BirthYear = todayYear - age;
if (year < BirthYear) {
Calc = (year - BirthYear) * (0 - 1);
message =
“The year " + year + " was " + Calc + " year before you were born”;
console.log(message);
} else if (year > todayYear) {
Calc = year - todayYear + age;
message = "You will be " + Calc + " in the year " + year;
console.log(message);
return "You will be " + Calc + " in the year " + year;
} else if (year < todayYear && year > BirthYear) {
Calc = year - todayYear + age;
message = "You were " + Calc + " in the year " + year;
console.log(message);
}
}
howOld(10, 1980);

// Once your function is written, write function calls to test your code!
So the console will return the right answers but when i use return with variables or strings it wont return anything.
Can someone explain what I am misunderstanding? Is it because i turned the date into a string?

// Write your function here: const howOld = function(age, year) { const currentYear = 2022; //Baseline Year const calculatedAge = year - currentYear; // desired Year minus 2022 const finalAge = age + calculatedAge; if (finalAge < 0) { return `The year ${year} was ${Math.abs(finalAge)} years before you were born`; } else if (calculatedAge > age) { return `You will be ${finalAge} in the year ${year}`; } else { return `You were ${finalAge} in the year ${year}`; } } // Once your function is written, write function calls to test your code! console.log(howOld(27, 1970));

Hello, this is my code and when i try submitting it, it says it is wrong. I cant find what it doesnt like. Can someone please help?

Can someone please help me by giving me pointers on a few things about this project? I’m struggling with how I’m supposed to come up with some of these variables on my own along with their values. such as let dateToday = new Date();
let thisYear = dateToday.getFullYear(); and const yearDifference = year - thisYear
const newAge = age + yearDifference. this all seems too advanced for me I just feel like I’m not yet capable of thinking this way and coming up with it all on my own. How can I start working towards that and what has helped you. Any tips on what helped with the project?

Read up on the JS special Date() function and its object constructor, Date() Constructor. That will give you a better understanding of the above statement (assignment).

dateToday will be a new object with complete set of properties for year, month, date, day, hour, minute, second, millisecond that are specific to the exact time (on your system) of the object instance. In this case the only property of interest, is the year.

The Date() object has methods in its protocol object for accessing each of the value properties mentioned above. Those methods are inherited by the instance, so,

dateToday.getFullYear()

will return the year, in this case, 2022.

The use of it in this exercise is moot since we can manually enter that year in our assignment; however, in a dynamic use case, meaning we could run the program in any year, it makes good sense to have our program retrieve the year information from our computer.

Thank you for the reply.

1 Like

Hello All…!

  1. The answer works out every time.
  2. I checked with the solution code after pulling my hair out for an hour and it matches.
  3. I even tried both concatenation and interpolation (left that one in, but commented out.

I continue to get the error:
“If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’” …

I can’t figure out for the life of me why this is not passing the Codecademy test:

// Write your function here: const howOld = (age, year) => { let currentDate = new Date(); let thisYear = currentDate.getFullYear(); const numOfYears = year - thisYear; const calcAge = age + numOfYears; if (year > thisYear) { return 'you will be ' + calcAge + ' in the year ' + year; //return `You will be ${calcAge} in the year ${year}`; } else if (calcAge < 0) { return `The year ${year} was ${-numOfYears} years before you were born`; } else { return `You were ${calcAge} in the year ${year}`; }; }; console.log(howOld(40, 2040));

Any help would be appreciated.

Thanks!

It might simply be that the exercise is very strict with the string formatting. Try capitalizing the first letter of the first word on line 10.

1 Like

This one was cool. Helped me get it right fast by going with pseudocode first, once I wrote down the logic I just implemented and… oh wait, syntax error, hehe. But those are easy to fix. My solution:

// Write your function here: const howOld = (age, year) => { // use the present as ${baseline} to calculate let date = new Date(); let currentYear = date.getFullYear(); // calculate ${year_of_birth} let yearOfBirth = currentYear - age; if (year > currentYear) { return 'You will be ' + (age + (year-currentYear)) + ' in the year ' + year; } else if (year < yearOfBirth) { return 'The year ' + year + ' was ' + (yearOfBirth - year) + ' years before you were born'; } else if (year < currentYear && year >= yearOfBirth) { return 'You were ' + (year - yearOfBirth) + ' in the year ' + year; } } /* 1. Calculate ${year_of_birth} 2. use the present as ${baseline} to calculate 3. if ${year} is > than ${baseline} return '${age} will be [x_age] in that ${year}' 4. if ${year} is < than ${year_of_birth} return '${year} was [x_num_of_years] before you were born' 5. if ${year} is < than ${baseline} && ${year_of_birth} and up return 'you were [x_age] in that ${year}' */ // Once your function is written, write function calls to test your code! console.log(howOld(12, 1969)) console.log(howOld(24, 2001)) console.log(howOld(47, 2035))

My solution:

theCurrentYear = 2023;

const howOld = (age, year) => {
const yearDifference = year - theCurrentYear;
const newAge = age + yearDifference;
if (yearDifference > 0) {
return You will be ${newAge} in the year ${year};
} else if (yearDifference < 0 && newAge < 0) {
return The year ${year} was ${newAge*-1} years before you were born;
} else if (yearDifference < 0 && newAge > 0) {
return You were ${newAge} in the year ${year};
}
};

Should I had use “theCurrentYear = 2023”; inside the function?

const howOld = (age, year) => {
  const currentYear = 2023;
  const yearBorn = currentYear - age;
  const newAge = Math.abs(year - yearBorn);
//if year is in the future 
if (year > 2023) {
  return `You will be ${newAge} in the year ${year}`
}
//if year is before they were born 
if (year < yearBorn) {
return `The year ${year} was ${newAge} years before you were born`
}
//if year is in past but after person born 
if (year < 2023) {
return `You were ${newAge} in the year ${year}`
}

};

console.log(howOld(32, 1999));
console.log(howOld(24, 2040));

I was SOO confused by this task, mainly because of the way it was worded on Codecademy. The logic made sense to me but I couldn’t seem to translate this into the correct syntax. I’ve spent hours staring at the same thing and reading this thread to try to understand, so I have the above code.

I was definitely overthinking it.

Hey Juan, I think so. Were you getting an error? Mine worked with the current year in the block code, see below my code snippet which works. I am realllyyyy new to this though, and honestly, I get stumped all the time, so I don’t think you should take my advice.

I just saw that no-one replied to you so I thought I would :slight_smile:

Hello All,

This gave me a bit of trouble. Not because my code was not working but because it was not matching the expected output. The out put for all of the calculated ages should be a positive number.

//Change Log
// Modified else if (newAge < age) to else if (newAge < 0)
//Modified return The year ${year} was ${newAge} years before you were born; to
//return The year ${year} was ${newAge * -1} years before you were born;

//This is the code that passed the exercise. //Change Log // Modified else if (newAge < age) to else if (newAge < 0) //Modified return `The year ${year} was ${newAge} years before you were born`; to //return `The year ${year} was ${newAge * -1} years before you were born`; const howOld = (age, year) => { let dateToday = new Date(); let thisYear = dateToday.getFullYear(); 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 * -1} years before you were born`; } else { return `You were ${newAge} in the year ${year}`; } }; // Function Calls console.log(howOld(53, 2042)); console.log(howOld(53, 1900)); console.log(howOld(53, 1985)); //This is the original const howOld2 = (age, year) => { let dateToday = new Date(); let thisYear = dateToday.getFullYear(); const yearDifference = year - thisYear; const newAge = age + yearDifference; if (newAge > age) { return `You will be ${newAge} in the year ${year}`; } else if (newAge < age) { return `The year ${year} was ${newAge} years before you were born`; } else { return `You were ${newAge} in the year ${year}`; } }; // Function Calls console.log(howOld2(53, 2042)); console.log(howOld2(53, 1900)); console.log(howOld2(53, 1985));

I hope this helps.

My code executes properly in both versions of my code, but I stll get an error message in the first version and doesn’t let me continue.

  1. Version
    Here is my code…the math seems right, so what am I doing wrong?
const howOld = (age, year) => {
  const birthYear = 2004;
  const currentYear = birthYear + age;
  if (year > currentYear) {
    let futureAge = (year - currentYear) + age;
    return `You will be ${futureAge} in the year ${year}`;
  } else if (year < birthYear) {
    let yearsBeforeBirth = birthYear - year;
    return `The year ${year} was ${yearsBeforeBirth} years before you were born`;
  } else if (year >= birthYear && year <= currentYear) {
    let pastAge = year - birthYear;
    return `You were ${pastAge} in the year ${year}`;
  }
};

console.log(howOld(20, 2024));
console.log(howOld(20, 2025));
console.log(howOld(20, 2023));
console.log(howOld(20, 2003));
console.log(howOld(20, 2016));
  1. Version
    In this version of my code I use the varibles from the HINT(and made some changes in the code obviously), the math seems right again, and Codecademy accepts this version.
const howOld = (age, year) => {
  const currentYear = 2024;
  const yearDifference = year - currentYear;
  const newAge = age + yearDifference;
  if (year > currentYear) {
    return `You will be ${newAge} in the year ${year}`;
  } else if (year < currentYear - age) {
    return `The year ${year} was ${(currentYear - age) - year} years before you were born`;
  } else if (year > currentYear - age) {
    return `You were ${year - (currentYear - age)} in the year ${year}`;
  }
};

// Once your function is written, write function calls to test your code!
console.log(howOld(20, 2024));
console.log(howOld(20, 2025));
console.log(howOld(20, 2023));
console.log(howOld(20, 2003));
console.log(howOld(20, 2016));

But what is the problem with the first version of my code? Both give me the same results as you can see…I just want to understand what I did wrong there.

I have checked and for me both of your versions worked :-?

That is so weird. Maybe there is some kind of bug on Codecademys side…strange

Hey everyone,
I am new to this:

Here are my solutions to this challenge, I wonder what is wrong with either, as they seem to print the correct results.

ONE:
const howOld = (age, year) => {

            // SET UP
            let date = new Date(); 
            let currentYear = date.getFullYear(); 
            let difference = year - currentYear; 
            let bornYear = currentYear - age; 

            // HANDLING CASES
            if (year > currentYear) {
                return `You will be ${age + difference} in the ${year}`; 
            }   else if (year < (currentYear - age)) {
                return `The year ${year} was ${currentYear - year - age} years before you were born`; 
            }   else if (year < currentYear && year >= bornYear) {
                return `You were ${age - (currentYear - year)} in the year ${year}`; 
            }         

}

TWO:

console.log(howOld(25, 2030));
console.log(howOld(25, 1990));
console.log(howOld(25, 2010));
console.log(howOld(25, 1999));

const howOld = (age, year) => {
SET UP
let date = new Date();
let currentYear = date.getFullYear();
let bornYear = currentYear - age;

      if (year > currentYear) {
          return `You will be ${year - bornYear} in the ${year}`; 
      }   else if (year < bornYear) {
          return `The year ${year} was ${bornYear - year} years before you were born`; 
      }   else if (year < currentYear && year > bornYear || year === bornYear) {
          return `You were ${year - bornYear} in the year ${year}`; 
      }

}

console.log(howOld(25, 2030));
console.log(howOld(25, 1990));
console.log(howOld(25, 2010));
console.log(howOld(25, 1999));

P.S Apologies for the messy post. I’d be glad to get some feedback.