why doesn't the howOld() function work?
#1
So I'm doing this code academy exercise and I realize that its not really working. I think at this point its more about a concept I am missing than about syntax. But I don't see the point of this exercise and so that's why I'm asking.

What is the point of this exercise and why is it I can't get the howOld function to work?

Quote: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 the person was born, you should return a string in the following format:

'You were [calculated age] in the year [year passed in]'

So based on that, here is my code:

Code:
function howOld(age, year) {
  if (year > Date().getFullYear()) {
      var ageAtYear = year + (Date().getFullYear() - age);
      return `You will be ${age} in the year ${year}`; // Will this calculate my age in the future?
  } else if (year > Date().getFullYear()) { // Will this really be the case where I have not been born yet?
      var yearsBeforeBorn = age - (Date().getFullYear() - year); // Missing semicolon.
      return `The year ${year} was ${yearsBeforeBorn} years before you were born`;
  } else {
    return `You were ${age} in the year ${year}.`; // Will this calculate my age in the given year? Missing curly brace and semicolon.
  }
}
Reply
#2
Ignoring the syntax issues, your logic is incorrect

Quote: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.

In your code you have three cases

1. If the year is in the future

I see two issues here, first your `ageAtYear` calculation and second your output. The second is easy to explain, you calculate `ageAtYear` but use `age`.

Lets look at the `ageAtYear` calculation though.

Code:
var ageAtYear = year + ((new Date()).getFullYear() - age);

Imagine we called howOld(10, 2030). so we are asking the question how old would someone who is current [10] be in the year [2030].

ageAtYear = 2030 + (2020 - 10) = 2030 + 2010 = 2040 which is obviously incorrect.

You need to rethink how to calculate what age someone will be.


2. The case where the year is before the current year

I assume that is what you are checking for, but your condition is incorrect, you should use '<' not '>'.

Your also assuming that if the year is before the current year then its also before they were born, which is not true.

howOld(10, 2015) for example should return "You were 5 in the year 2015"

Something like howOld(10,1990) would be the before they were born case returning "The year 1990 was 20 years before you were born."

So you're not handliong this case correctly.

3. When the target year is the current year

This should work fine once you fix the condition for the second case, though you could also remove it and handle it in the second case and use '<=' instead.


Quote:I think at this point its more about a concept I am missing than about syntax. But I don't see the point of this exercise and so that's why I'm asking.

Seems like the point of it is to get you thinking about how to reason about what the code will do with conditionals and how to break a problem down into appropriate conditions, its similar to the class FizzBuzz test.

Lastly, I will point out that the third case includes a '.' at the end of the sentence, I'm not sure how strict code academy is on the output format but that can cause issues since the problem statement doesn't include a period.
Reply
#3
I updated it but I'm still getting issues:

Code:
var date = new Date();
var currentYear = date.getFullYear();
function howOld(age, year) {
  if (year > currentYear) {
      var ageAtYear = year - (currentYear - age);
      return `You will be ${ageAtYear} in the year ${year}`; // Will this calculate my age in the future?
  } else if (year > currentYear) { // Will this really be the case where I have not been born yet?
      var yearsBeforeBorn = (currentYear - age) - year;
      return `The year ${year} was ${yearsBeforeBorn} years before you were born`;
  } else {
    return `You were ${age} in the year ${year}.`; // Will this calculate my age in the given year? Missing curly brace and semicolon.
  }
}

Is there a syntax error that I'm missing?
Reply
#4
(04-04-2020, 04:28 AM)QMark Wrote: Is there a syntax error that I'm missing?

I've explained several times in this thread how to look for syntax errors. So why dont you tell us if there is and we can go from there.
Reply
#5
(04-04-2020, 05:02 AM)MuddyBucket Wrote: [quote="QMark" pid='17120' dateline='1585974501']
Is there a syntax error that I'm missing?

I've explained several times in this thread how to look for syntax errors. So why dont you tell us if there is and we can go from there.
[/quote

Ok, there is some sort of syntactical error I get in my debugger and here are the errors I get:

Code:
"missing 'use strict' statement"
"Expected var at column 9, not column 7"
"Expected ` at column 9, not column 14"
"Unexpected `"

I look this stuff up and I get results like this one which doesn't tell me anything about my code by comparison:

https://stackoverflow.com/questions/3532...pected-var

Why am I getting "expected var at column 9?" And shouldn't it say "row 9?"

Oh, and I ask dumb questions that I don't really mean compulsively even when I know they are dumb because of my autism so I'm glad you pointed that out to me. I'm working on it.
Reply
#6
I might be starting to lose it. I swear I responded to this thread. Must have been another one on the same thing? Or I'm going crazy.

Anyways...

Your code looks fine to me. There's no syntax error. Sort of.

The piece of shit debugger you're using is checking your formatting. Looks to me like you're indenting by 2 spaces. It's expecting a 4 space indentation from the looks of it.

So this code is syntactically correct - but formatted poorly. most browsers and other applications won't give a shit. You could write the code on one line if you wanted to. But this debugger is forcing you to make it look all "purty"
Reply
#7
(04-04-2020, 06:23 AM)MuddyBucket Wrote: Your code looks fine to me. There's no syntax error. Sort of.



So this code is syntactically correct - but formatted poorly. most browsers and other applications won't give a shit. You could write the code on one line if you wanted to. But this debugger is forcing you to make it look all "purty"

So then why doesn't code academy accept the code if it technically works? Should I contact code academy and ask them? I mean maybe I'm doing something wrong with the instructions of the exercise.

(04-04-2020, 06:23 AM)MuddyBucket Wrote: The piece of shit debugger you're using is checking your formatting. Looks to me like you're indenting by 2 spaces. It's expecting a 4 space indentation from the looks of it.

But I think code academy would accept it if that's all that was wrong, no?

This is the instructions for the exercise:

Code:
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:
[list]
[*]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]'

[/list][list=1]
[*]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 the person was born, you should return a string in the following format:
'You were [calculated age] in the year [year passed in]'

[/list]

Am I doing what code academy wants, and if so why won't they accept my code?
Reply
#8
(04-04-2020, 06:15 PM)QMark Wrote: So then why doesn't code academy accept the code if it technically works?

Because just giving them syntactically correct code that will run, does not pass the test. You need to give them syntactically correct code that ALSO outputs the values they're expecting.

(04-04-2020, 06:15 PM)QMark Wrote: Should I contact code academy and ask them? I mean maybe I'm doing something wrong with the instructions of the exercise.

100% You're not following the instructions of the exercise. I don't even need to look at the instructions to know that.

Quote:var date = new Date();
var currentYear = date.getFullYear();
function howOld(age, year) {
  if (year > currentYear) {
  } else if (year > currentYear) { // Will this really be the case where I have not been born yet?
  } else {
  }
}

two out of 3 of your condition statements are literally identical. So there is no possible way to get 3 different outputs from this code like they're asking. So the logic of the code is wrong. I believe dropzone already mentioned this, and other issues in his response, that you haven't addressed yet.
Reply
#9
I FINALLY SOLVED IT!!!

Code:
const date = new Date();
const currentYear = date.getFullYear();
function howOld(age, year) {
  const yearDifference = year - currentYear;
  const newAge = age + yearDifference;
  const yearOfBirth = currentYear - age;
  const yearsBeforeBorn = yearOfBirth - year;
  if (year > currentYear) {
      return `You will be ${newAge} in the year ${year}`;
  } else if (yearOfBirth > year) {
      return `The year ${year} was ${yearsBeforeBorn} years before you were born`;
  } else {
    return `You were ${newAge} in the year ${year}.`;
  }
}

I MADE IT WORK!!!

From now on I'm always gonna map this stuff out on paper before coding it!!!

Thanks guys!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to split this JS function into smaller ones of make function work? QMark 0 344 05-01-2020, 10:58 PM
Last Post: QMark
  why is my function evaluating to null? QMark 10 2,266 04-08-2020, 12:41 AM
Last Post: QMark
  Why learn PHP? Syntax 2 6,215 12-05-2015, 03:34 PM
Last Post: Syntax