FAQ: Code Challenges: JavaScript Fundamentals - howOld()

Ok so Im not gonna lie, this one really confused me and a lot of it is the wording of the task, the open-endedness of all the plethora of options to attempt it, along with trying to make sure all syntax was perfect along the way. To me its a bit of a time waste to have us so focused on perfect syntax during this early learning phase, because the concepts and output can be correct but were getting tied down in trying to have perfect syntax. VS code style syntax “spellcheck” would really help here but I get that we do need to understand what perfect Syntax is and be able to write it.
However, I also think these first challenges like this would be a bit better done with wide ranging multiple choice code blocks, the way the app quizzes are structured. Simply because this early in learning I think its best to first learn the concepts by putting premade blocks of code together, so I can understand EXACTLY how they look when done correctly. If I try doing it perfectly before Ive seen it done perfectly, from scratch… it just takes forever and I end up rabbit holing down the wrong paths for awhile before looking for help. Ill be studying this code, along with the others posted here to get a grasp on why certain methods worked and others didnt. We havent been taught the dateToday.getFullYear methods so although this code works, Im not sure how we were supposed to reproduce it from scratch at this point of our learning.

(upload://1SYBXjtknZsXltyTflR6uLrMjrC.jpeg)

const howOld = (age,year) => {

let currentYear = 2020;

let YearBorn = 1994;

const yearPassedIn = year - currentYear;

const calculatedAge = age + yearPassedIn;

const yearPast = YearBorn - year;

if (year > 2020) {

return `You will be ${calculatedAge} in the year ${year}`;

} else if (year < YearBorn) {

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

} else if (YearBorn > year < currentYear) {

return `You were ${calculatedAge} in the year ${year}`;

}

}

Fails even though it clearly works… Will find the correct code that matches the exercises “expectations” in the forum and copypaste it…

Hi partners!
I’m lost. I don’t know why my code isn’t working.
Could you help me?

function howOld(age, year){
const currentYear = 2020;
const yearDifference = currentYear - year;
const newAge = age + yearDifference;
const yearsBefore = yearDifference - age;
if (yearDifference > 0){
  return 'You will be ' + newAge + ' in the year ' + year;
}
  else if (yearDifference < 0){
 return 'The year ' + year + ' was ' + yearsBefore + ' was years before you were born';
  }
  else if (yearDifference === 0){
      return 'You were ' + newAge + ' in the year ' + year;
  }
}

Thanks!!!!

I am so stuck - yet I don’t want to see the solution just yet.
I spent a lot of time following the logics of the instructions, but I get the result of ‘undefined’.
If anybody out here sees any problem let me know! Thanks in advance.

const currentYear = 2020;

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

Is there anything wrong with my code? I got it working in that putting in various years and ages gives the right results but it’s not passing the exercise. Guessing the exercise is expecting a different solution, or have I done something wrong which somehow doesn’t break the code?

// Write your function here:

const theCurrentYear = 2020
const howOld = (age, year) => {
if (year > theCurrentYear) {
age = (age + (year - theCurrentYear))
console.log(You will be ${age} in the year ${year})
}
else if ((year + age) < theCurrentYear) {
yearDifference = (theCurrentYear - (year + age))
console.log(The year ${year} was ${yearDifference} years before you were born) }
else if (year <= theCurrentYear) {
age = age - (theCurrentYear - year)
console.log(You were ${age} in the year ${year})
}
}

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

howOld(33, 2030)

Welcome, @jamesbandenburg02904,

The thing to do first is work out the steps on paper working with, say, your birth year (derived from age) and the current year.

  • year is before current year
  • year is after current year
  • year is current year

Is the exercise asking us to log inside the function, or are we to return the resulting string? The preferred approach would be to return, and then log at the caller.

Please post a link to the exercise in a reply so we can clarify the instructions.

This is the code I have used:

const howOld = (age, year) => {
  var d = new Date();
  var currentYear = d.getFullYear();
  let calcAge = age + (year - currentYear);
  let positiveAge = Math.abs(calcAge);

  return (currentYear < year) ? `You will be ${calcAge} in the year ${year}` :
  (calcAge < 0) ? `The year ${year} was ${positiveAge} years before you were born` :
  `You were ${calcAge} in the year ${year}`
}

Creating test for every scenario helped me in finding the correct code.

Tests I’ve used:
console.log(howOld(29, 2030)); //Future

console.log(howOld(29, 1995)); //After born

console.log(howOld(29, 1980)); //Before born

Hope this helps

Hey guys!
I am in need of some feedback :slight_smile:

I don’t understand really what the problem with my code is:

// Write your function here:

const howOld = (age, year) => {
const currentYear = 2020
let yearOfBirth = currentYear - age
let yearDifference = yearOfBirth - year
if (yearOfBirth > year) {
return The year ${year} was ${yearDifference} years before you were born
} else if (yearOfBirth < year && year < currentYear) {
return You were ${(currentYear - year)} in the year ${year}
} else if (year > currentYear) {
return You will be ${year - yearOfBirth} in the year ${year}
}
}

The error states the following: If the year passed in is in the past, but not before the person was born, the function should return: ‘You were [calculated age] in the year [year passed in]’

If I test-run the code, it produces the exact line, with the correct calculations.
What’s wrong with it? Can someone help, please? :slight_smile:

For future posts, please format your code using the </> button.

Take a look at your return statement here. Your calculations are actually incorrect. We want to return the age of the user in the year year. currentYear - year is the difference between 2020 and the year passed in. You’re partway there; what change do you need to make to return the age of the user in the year year rather than the difference in years between year and 2020?

1 Like

Thank you for your answer!
Would the change actually be the + age?

Not quite, maybe try working out the problem with pen and paper to see what combination of variables gets you to the right answer.

Solution

Since we want the user’s age in year, we need to subtract the difference between currentYear and year from the user’s current age. The difference between currentYear and year gives us the number of years older the user is since year. By subtracting this difference from age, we get the age of the user in the year year. Therefore, the expression should be ${age - (currentYear - year)}.

Hey there, I’m a little stuck with passing the exercise. My code seems to work although it can come out with “you were age 0”. I wouldn’t say that conversationally, but I don’t feel like it’s wrong on the context of this exercise:

This is the exercise

Here’s my code:

// Write your function here:
const howOld = (age,year) => {
  let calculatedAge = age - 2021 + year
  let yearPassedIn = year
  let calculatedNumberOfYears = Math.abs(age - 2021 + year)
  if (calculatedAge < 0) {
    return `The year ${yearPassedIn} was ${calculatedNumberOfYears} years before you were born`;
  } else if (yearPassedIn > 2021) {
    return `You will be ${calculatedAge} in the year ${yearPassedIn}`
  }
  return `You were ${calculatedAge} in the year ${yearPassedIn}`
}

// Once your function is written, write function calls to test your code!
console.log(howOld(30,2050))
console.log(howOld(10,1700))
console.log(howOld(1,100))
console.log(howOld(50,1971))
console.log(howOld(50,1990))

Any help would be much appreciated, thank you
:heart:

It’s just passed! Not sure what I changed

can someone help me understand why my solutions does not work?

const howOld = (age, year) => {

var currentDate = new Date();

var currentYear = currentDate.getFullYear()

var birthYear = currentYear - age

if (birthYear < year) {

return The year ${year} was + (birthYear - year) + ‘years before you were born.’;

};

if (currentYear < year) {

let future = (year - currentYear) + age;

return You will be ${future} in the year ${year}.;

};

if (currentYear > year && year > birthYear) {

let past = (currentYear - year) - age

return You were ${past} in the year ${year}.;

};

};

Examine that for correctness. Shouldn’t the relation be the other way around (year < birthYear)?

Hello everyone, I’m stuck and need help! The following code is bringing back "You were NaN in the year 2050. It should be returning “You will be 67 in the year 2050”. Can anyone please tell me why it is doing this?

const howOld = (age, year) => {

const yearDifference = year - theCurrentYear

var theCurrentYear = 2021

const newAge = age + yearDifference

if (newAge < 0) {
console.log('The year'  + year + ' was ' + -newAge + ' years before you were born');
} 
else if (newAge > age) {
   console.log('You will be ' + newAge + ' in the year ' + year);
} 
else {
  console.log('You were ' + newAge + ' in the year ' + year);
}
}

howOld(37, 2050)

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

can someone explain to me why this code is not acceptable by the website? It works whenever i plug in numbers and calculates correctly.

What error message are you getting?

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

Could it be that your current year is expected to be 2021? (Just a stab in the dark.)