FAQ: Code Challenges: JavaScript Fundamentals - howOld()

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?

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.

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


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

I need some help, my code is working perfectly but I still get this message.
thanks in advance.

Hello, @gaby_bendfinger.

Welcome to the forums. I can’t copy, and run your code since it’s in a screenshot, but running through it in my head, the logic seems correct. The problem is likely that you have single quotes ' ' inside your back ticks, so your output is:

‘bla bla bla’

instead of

bla bla bla

The SCT for the lesson probably isn’t expecting the quotation marks.

Thanks midlindner!

I appreciate the help, it works!

1 Like

I’m getting an error and can’t figure out why. I’d love help if possible.

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


error = If the year passed in was before the person was born, the function should return: ‘The year [year passed in] was [calculated number of years prior to their birth] years before you were born’

Hello, @net6465339463, and welcome to the forums.
In the string you are returning below, you have already calculated the correct value for the number of years before the user was born, and assigned it to the variable newAge. Not sure if using ${newAge} in place of ${bornYear - year} will clear the error. It appears you are producing the correct output.

Edit: I just looked at the exercise, and the SCT for the lesson checks your code using howOld(3, 1998). Try it, and you’ll see the expected output is not returned.

I’m getting this error message when I run my code:

If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’

My code looks like this:

// Write your function here:
const howOld = (age, year) => {
  const thisYear = 2019;
  const birthYear = thisYear - age;
  if (year > thisYear) {
    return `You will be ${year - birthYear} in the year ${year}`;
  } else if (year < birthYear) {
    return `The year ${year} was ${birthYear - year} years before you were born`;
  } else {
    return `You were ${year - birthYear} in the year ${year}`;

// Once your function is written, write function calls to test your code!

Running my three test function calls seems to suggest I have the correct solutions:

You will be 50 in the year 2029
The year 1969 was 10 years before you were born
You were 10 in the year 1989

I seem to have a habit of missing the obvious stuff…anyone give me a hint?

EDIT original had wrong code block

Hello, @laconicsky.

Your code does product correct results. The issue is that the SCT for the lesson is written to get the current year, and assign it to the value of thisYear. You are using 2019, but the SCT is using 2020. If you change your code to assign 2020 to thisYear, it should pass the SCT. Obviously this exercise doesn’t account for whether or not the person specified has already had their birthday or not in the current year. :wink:

1 Like

What does SCT stand for?
And yeah I think I just forgot it was 2020…

SCT is Submission Correctness Test. You can actually view the test.js file for this particular exercise by clicking on the folder icon in the top left corner of the editor, and clicking on it.

1 Like

Thanks that’s really useful to know.

Because the Date object has probably not been introduced yet, we won’t think to use it to generate the current year. It helps to make our program more dynamic, though.

let currentYear = new Date.getFullYear()

That’s correct - on the web dev path we haven’t come across Date yet. Useful to know though thanks.

1 Like

// Write your function here:

const howOld = (age, year) => {
let currentYear = 2020;
if (year >= currentYear) {
return You will be ${(year - currentYear) + age + 1} in the year ${year};
} else if ((currentYear - year) > age) {
return The year ${year} was ${currentYear - age - year - 1} years before you were born;
} else {
return You were ${-1*(currentYear - year - age)+1} in the year ${year}

// Once your function is written, write function calls to test your code!
console.log(howOld(36, 2023));
console.log(howOld(36, 1983));
console.log(howOld(36, 1985));
console.log(howOld(36, 1966));

You will be 40 in the year 2023
The year 1983 was 0 years before you were born
You were 2 in the year 1985
The year 1966 was 17 years before you were born

this is my code, without any additional variables, it’s working fine, why i can’t go to next exersise?