FAQ: Code Challenges: JavaScript Fundamentals - howOld()

This community-built FAQ covers the “howOld()” exercise from the lesson “Code Challenges: JavaScript Fundamentals”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise howOld()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

3 posts were split to a new topic: Code should return a string not log it

6 posts were split to a new topic: Troubleshooting my code?

2 posts were split to a new topic: I forgot to double check the string text

3 posts were split to a new topic: When should I use var, let or const?

A post was split to a new topic: Anyone know why this doesn’t work?

7 posts were split to a new topic: Troubleshooting my code?

This is what I used…

const howOld = (age, year) =>{
  let currentYear = 2019;
 if (year >= currentYear) {
   return `You will be ${age + (year - currentYear)} in the year ${year}`
 }
  
  else if (year <= currentYear && year <= (currentYear - age)){
    return `The year ${year} was ${ (currentYear - age) - year} years before you were born`
  }
  
  
  else if ((year <= currentYear) && (year >= (currentYear - age))){
    return `You were ${year -(currentYear - age)} in the year ${year}`
  }
}
6 Likes

6 posts were split to a new topic: Math mistake

I just want to point out that (currentYear - age) only gives you your birth year if your birthday has already occurred this year.

For example:

I am 25 right now (June, 2019). My birthday is in July. If I subtract my age from the current year (2019 - 25) the result is 1994, when I was actually born in 1993.

The problem is I am actually ~25.9 years old, but we are prone to think about age as an integer.

This affects the logic required for the solution.

2 Likes

I am also getting an error message. I first did it the way I have commented out, then used the variables suggested in the hint message and still getting an error.

Both of my versions seem to work so it’s all good

3 posts were split to a new topic: Did I do my math wrong?


Everything is working as intended yet I still get this error message. PLEASE HELP!

Hello, @mattparadoxx.

Your 2nd output should read, “The year 1985 was 21 years before you were born” without the -.

Why is it doing that, I can’t figure it out?

1 Like

The way your code is written:
1985 - 2019 = -34 (yearDifference)
13 + -34 = -21 (newAge)

There are a couple of options to produce the expected output. If you are familiar with the Math.abs() method, you can get the absolute value of newAge. Otherwise you could find the birth year by subtracting age from theCurrentYear, and then subtract the year parameter from the birth year, and use that value in your return statement instead of the newAge as you currently have it.

1 Like

One line of thinking is to always subtract from the current year.

2019 - 1985 => 21 years (positive; greater than zero)

2019 - 2030 => -11 years (negative; less than zero)

We can make the program smarter by not supplying a fixed value for current year.

from datetime import datetime

currentYear = datetime.now().year
1 Like

Hello,

My code below follows all the rules and outputs when changing the parameters, but I am still getting the error message when hitting submit. I’ve looked it over a LOT and can’t seem to pinpoint the issue.

Could someone help me out?

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

console.log(howOld(25, 2001));

If I run your code using:

console.log(howOld(25, 1970));

I get:

The year 1970 was 49 years before you were born

However, If a person is 25 now, they were born in 1994, and 1970 was 24 years before they were born.

omg always the little mistakes! Thank you so much I got it to work now :slight_smile:

1 Like