How to get a positive year difference printed


#1

https://www.codecademy.com/paths/web-development/tracks/getting-started-with-javascript/modules/code-challenge-javascript-fundamentals/lessons/javascript-fundamentals-code-challenge/exercises/how-old

Hi, on exercice 12 of code challenges, Javascript fundamentals, I wrote my code:

const howOld = (age, year) => {
const yearDifference = year - 2018;
const newAge = age + yearDifference;

if (newAge < 0){
return The year ${year} was ${yearDifference} 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(20, 1000))

But i want to get rid of the minus when it prints the first condition (newAge < 0). How do I do that?

it prints : “The year 1000 was -1018 years before you were born”


#2

use Math method.
Math.abs(newAge)


#3

Something tells me the above reply is from somebody who did not do this exercise.

Here is a spoiler to study. Note that some of the math is done inside the conditional return statements.

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

// Once your function is written, write function calls to test your code!
console.log(howOld(19, 2020))
console.log(howOld(19, 1995))
console.log(howOld(19, 2005))

#4

Yes! I need newAge variable to be able to be a negative (or positive value) in the math, but I need it to print positive always lol

A solution ?


#5

Start by making your time difference pivot around the current year. That means subtracting from that number to obtain the difference. A positive difference moves us left on the number line; a negative difference moves us right.

If we consider all the years since we were born up to the present year as the domain, and have a whisker that extends to the left of that to some past date outside of our domain, and a whisker that extends to the right to some future date not in our domain, we can visualize this problem.

There are three intervals that have their own mathematical definition.

[
  ['years before birth'], ['years since birth'], ['years in the future']
]

Study the spoiler and you will see how they each play out. There is no one expression that can solve this problem.


Okay, I can see an argument brewing (figuratively speaking) and know that there is a fixed value that we can use. And it goes right back to the post that I dissed earlier. The absolute value. In the earlier spoiler we had direct influence on the arithmetic in each interval, for the purpose of expressing it as a positive number.

x - a = | a - x |

That’s what we were literally doing, arbitrarily. You are seeking this one value that can be expressed always as a positive number, and forego the ordering in the arithmetic. Makes sense.

const howOld = (age, year) => {
  var currentYear = new Date().getFullYear();
  var birthYear = currentYear - age;
  var diff = Math.abs(birthYear - year);
  if (currentYear - year < 0) {
    return `You will be ${diff} in the year ${year}`;
  }
  if (year < birthYear) {
    return `The year ${year} was ${diff} years before you were born`;
  }
  return `You were ${diff} in the year ${year}`;
};

My diff is likely your newAge.


Addendum

Why did I use a Date object?

Simple. This program will work forever.