0

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.

  • 3
    `newAge = Math.abs(age + yearDifference)` will *only* return a positive number, so `positiveNewAge = newAge * -1` will *only* produce a negative number. – VLAZ Oct 13 '20 at 15:20
  • So if I put positiveNewAge = Math.abs(newAge) it should come out as a positive number right? – mbmarketing4you Oct 13 '20 at 15:21
  • Nevermind. I swear I tried that a couple of minutes ago and it didn't work. – mbmarketing4you Oct 13 '20 at 15:23
  • 1
    https://shouldiblamecaching.com/ :P – VLAZ Oct 13 '20 at 15:24
  • XD probably. Thanks for your help. I knew that's how I needed to do it. Just wasn't working. Is there a better way to write the code though? or is that the best way. – mbmarketing4you Oct 13 '20 at 15:25
  • 1
    That's honestly the best way. It's probably possible to do it a bit shorter but it's not going to be as readable and maintainable. I'd stick to this. Much easier to debug and understand. – VLAZ Oct 13 '20 at 15:26
  • That's great to hear. I thought maybe I was doing it the hard way. lol – mbmarketing4you Oct 13 '20 at 15:27
  • I have another function problem that I am having a hard time with. Do you mind hopping on the chat to talk through it with me? – mbmarketing4you Oct 13 '20 at 16:17
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/222974/discussion-between-mbmarketing4you-and-vlaz). – mbmarketing4you Oct 13 '20 at 16:21

1 Answers1

0

Thanks to @vlaz for making me try this again. Here is the answer I came up with after trying this again.

function howOld (age, year) {
  let theCurrentYear = 2020;
  const yearDifference = year - theCurrentYear;
  const newAge = age + yearDifference;
  const positiveNewAge = Math.abs(newAge);
  
  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}`;
  } else if (year === theCurrentYear) {
    return `You are ${age} in the year ${2020}`;
  }
};

console.log(howOld(28,1991));