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 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;

//console.log(YearBorn);

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}.;

}

};

console.log(howOld(32,2000));

console.log(howOld(32,2050));

console.log(howOld(32,1950));

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}`;
  }

}

console.log(howOld(25,1995))

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.

Hello,

I’ve tried everything but can’t pass this test. My mind is melting on this. I wrote everything in pseudo code and implemented it, it’s not perfect but I think I more or less got it. I can’t tell where I’m wrong, my code seems to run fine when testing it with different values but I keep getting an error saying “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’”

Can someone give me a little clue on what’s wrong here ?

Thanks

// Write your function here:

const howOld = (age, year) => {
let currentYear = 2021;
let futureAge = age + (year - currentYear);
let yearBorn = currentYear - age;
let calculatedNumberOfYears = year - yearBorn;
let calculatedPastAge = year - yearBorn;
if (year > currentYear) {
  return `You will be ${futureAge} in the year ${year}`;
} else if (year < yearBorn) {
  return `The year ${year} was ${calculatedNumberOfYears} years before you were born`;
} else if (year < currentYear && year > yearBorn) {
  return `You were ${calculatedPastAge} in the year ${year}`
};
}
console.log(howOld(30, 2032));
console.log(howOld(30, 1920));
console.log(howOld(30, 1995));
// Once your function is written, write function calls to test your code!

I used the following code. Which as far as I can tell works perfectly but is not written the way the exercise wants me to. Is there a flaw I can’t see in this? Any feedback is greatly appreciated.

const howOld = (year) => {

let birthYear = 1992;
let calculatedAge = (year - birthYear);

if (year > 2021)
return You will be ${calculatedAge} in the year ${year};

else if (year < 1992)
return The year ${year} was ${Math.abs(calculatedAge)} years before you were born

else if (year > 1992 || year < 2021)
return You were ${calculatedAge} in the year ${year};

}

console.log(howOld(2025));

We would rather read about your process than critique your code. Can you take us through the code?

So I didn’t fully understand what was asked of me until I read the solution.
I thought the howOld function was to have only one parameter (year) because I figured we are trying to find the age not give the program both age and year. I didn’t realize that the age parameter was to give the computer the starting point to find the adjusted age after applying the year change.

My code only has one parameter (year) and this howOld function will give me my age based on the year I provide it with. These exercises are great, really get you thinking. And somehow I manage to learn something valuable even when I misunderstand the task.

1 Like

This made no sense to me. I had to cheat and look at the forums. Which I feel bad about and then that still didn’t make sense then looked at view solution still lost. Can someone please explain this to me? Walk me through this you like you would a 2 year old. Please.

2 Likes

What do we know initially? Answer: The current year. The computer can supply that.

currentYear = new Date().getFullYear();

We input our age so our birth year is easy to find…

birthYear = currentYear - age;

While we’re at it, we may as well compute age in year…

howOldInYear = year - birthYear;

If the year is in the future, we can compute our age in that year…

futureAge = howOldInYear;

If the year is before our birth year, then we can compute how many years before…

yearsBefore = birthYear - year;

The only thing we haven’t looked at is when year is between our birth year and the current year. We can compute our age in that year…

pastAge = howOldInYear
1 Like
const howOld = (age, year) => {
  const currentYear = new Date().getFullYear();
  const yearDiff = Math.abs(year - currentYear);
  let result; 

  if (year > currentYear) {
    result = `You will be ${age + yearDiff} in the year ${year}`;
  }
  else if (yearDiff > age) {
    result = `The year ${year} was ${currentYear - year - age} years before you were born`;
  }
  else if (age > yearDiff) {
    result = `You were ${age - yearDiff} in the year ${year}`;
  }
  return result;
};

The only issue I have with this solution is there isn’t a default catch all (which would result in undefined) for anything outside of the scenarios needed for the challenge. I could easily add something, but MVP (minimum viable product) is the goal and this works.

My thought process for this exercise is as follows:

  • I avoid multiple return statements (not sure if it’s as taboo in JS as other OOP languages but it has been drilled into me at work to avoid more than one if possible)
  • Taking the absolute value of the difference in year and currentYear allows me to reuse this number, I just need to be careful of the order of operations
  • JS allows for calculations inside ${variable} syntax, which cuts down on the number of variables that I have to declare.

Thanks,
Ryan

const howOld = (age, year) => {

// The following two lines make it so that our function always knows the current year.

let dateToday = new Date();

let thisYear = dateToday.getFullYear();

// It is totally ok if your function used the current year directly!

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} years before you were born`

} else {

    return `You were ${newAge} in the year ${year}`

}

}

Could anyone help me out? I’ve got the correct code but just not the same as the solution, but it won’t let me pass. I have just applied a different method in solving this but don’t know why codecademy won’t let me pass.

unction howOld(age, year) {

const currentYear = 2021;

if (currentYear < year) {

age = age + (year - currentYear)

return `You will be ${age} years old in ${year}.`

} else if (year < (currentYear - age)) {

const pastYears = currentYear - age - year

return `The year ${year} was ${pastYears} years before you were born.`

} else {

age = age - currentYear + year;

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

}

}

console.log(howOld(25, 2070)) // prints out You will be 74 years old in 2070.

On a side note, it is never advisable to modify the parameters.