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.

9
  • 3
    newAge = Math.abs(age + yearDifference) will only return a positive number, so positiveNewAge = newAge * -1 will only produce a negative number. Commented Oct 13, 2020 at 15:20
  • So if I put positiveNewAge = Math.abs(newAge) it should come out as a positive number right? Commented Oct 13, 2020 at 15:21
  • Nevermind. I swear I tried that a couple of minutes ago and it didn't work. Commented Oct 13, 2020 at 15:23
  • 1
    shouldiblamecaching.com :P Commented Oct 13, 2020 at 15:24
  • 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. Commented Oct 13, 2020 at 15:26

1 Answer 1

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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.