Taking some time to try and learn Javascript to become a better developer. I am having a bit of problem with this code.
function howOld (age, year) {
let theCurrentYear = 2020;
const yearDifference = year - theCurrentYear;
const newAge = Math.abs(age + yearDifference);
const positiveNewAge = newAge * -1;
if (newAge > age) {
return `You will be ${newAge} in the year ${year}`;
} else if (newAge < 0) {
return `The year ${year} was ${positiveNewAge} years before you were born`;
} else if (newAge > 0 && newAge < age) {
return `You were ${positiveNewAge} in the year ${year}`;
}
};
console.log(howOld(28,2019));
Here is what it should do:
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]'
The problem I am having is getting the newAge to be a positive number so I can pass it as a positive onto the string. I have tried Math.abs() and multiplying the newAge by -1 and I can't seem to get it to change to a positive number.
Thanks to @vlaz for making me try this again. Here is the answer I came up with after trying this again.