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