Code Challenges: JavaScript Fundamentals

I need some help with a coding challenge (link above). When I run the code, the answers all come out as they should but I’m still not able to complete the challenge as I get an error message. Please help!

Welcome to the forums. It will be helpful to others if we include a URL to the exercise page, for context, and the error message you are getting. Is it in the LE (an SCT error message) or in the console (an interpreter/compiler error)?

The link should be at the very top of my message but here it is again:

The error message is not in the console, it is in the middle section where I write my code in Codecademy. It says “If the year is in the future, the function should return ‘You will be [calculated age] in the year [year passed in]’”

Not the link we are after. We need the link to the CC exercise landing page so we can read the instructions and get full context of the problem.

I’m not sure how to do that, however these are the only instructions given:

Write a function, howOld() , that has two number parameters, age and year , and returns how old someone who is currently that age was (or will be) during that year . Handle three different cases:

  • If the year is in the future, you should return a string in the following format:
    ‘You will be [calculated age] in the year [year passed in]’

  • If the year is before they were born, you should return a string in the following format:
    ‘The year [year passed in] was [calculated number of years] years before you were born’

  • If the year is in the past but not before the person was born, you should return a string in the following format:
    ‘You were [calculated age] in the year [year passed in]’

Copy the URL in the location bar of your browser when you are on the exercise page.

https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-javascript-syntax-part-i/modules/fecp-practice-javascript-syntax-variables-data-types-conditionals-functions/lessons/javascript-fundamentals-code-challenge/exercises/how-old

Not sure you’ll be able to open it like that. I tried sending it to my sister (she also codes) and it just took her to the login page.

const now = new Date().getFullYear()

Above we let the system tell us what year it is so this code will be future proof. It’s never a good idea to include literals as running data unless they certain constants. Let a function or built in method fill the role of computing values to replace those literals, in this case, fixed year. Our now variable will always be current with every session, regardless how far into the future.

Let’s try writing out the three sentences without conditions. That is, compute each one independently…

const now = new Date().getFullYear()
age = 67
year = 1999
console.log(
`You will be ${year - now + age} in the year ${year}`
)
console.log(
`The year ${year} was ${now - age - year} years before you were born`
)
console.log(
`You were ${year - now + age} in the year ${year}`
)

Now we can read all three returns to verify the veracity…

You will be 45 in the year 1999
The year 1999 was -45 years before you were born
You were 45 in the year 1999

They are all true, in one form or another, but we can see which one might fit the input year better than the other two. My vote is for the last one.

We created all three statements while creating only one variable, now. After that we were able to express our computations using the expression field of the template literal. This is all fine and good, but how do we set up the conditions without using new variables (other than now)?

Let’s try.

if (year > now) { // it’s in the future }
if (year < now - age) { // it’s before our BD }
if (year < now) { // it’s in the past }

You will be 45 in the year 1999
The year 1999 was -45 years before you were born
You were 45 in the year 1999
if (now <= year) {
  console.log(
`You will be ${year - now + age} in the year ${year}`
  )
}

The above gives no output for the given parameters, but neither do we expect any. Check one off the list.

if (year <= now - age) {
  console.log(
`The year ${year} was ${now - age - year} years before you were born`
  )
}

That one gives no result, either, and as before, as expected.

This leaves us only one result which must be true and correct…

You were 45 in the year 1999

If we change the console.log() to return we will then log the return values, which will be one of those three. All computed without extra variables beyond the current year variable, now.


Aside

The step by step approach works even without creating new variables if we think it through. The closer we can get to writing code with fewer variables in play, the closer we get to writing expressive code which one will find has a real value in complex programs.

const howOld = function (age, year) {
  const now = new Date().getFullYear();
  if (now <= year) {
    return `You will be ${year - now + age} in the year ${year}`
  }
  if (year <= now - age) {
    return `The year ${year} was ${now - age - year} years before you were born`
  }
  return `You were ${year - now + age} in the year ${year}`
}