ga-wdi-boston/ruby-vs-js-array-methods

Age result is off by 1 between JS and Ruby

Opened this issue · 6 comments

During lab to calculate number of people older/younger than you, you get an off by one between the two results. Needs deeper look at age function in person.rb and person.js

via @jrhorn424

person.rb was incorrectly calculating the date when the person's birthday is the same month and day as the present date.

gaand commented

Does that mean the person.js is also miscalculating age?

Is this the solution:

-       dob.month == today.month && dob.day >= today.day
+       dob.month == today.month && dob.day > today.day
-      dob.getDate() >= today.getDate()) {
+      dob.getDate() > today.getDate()) {

Actually, yes, but no. The javascript function is working correctly because of a Different error. Javascript 0 indexes months (but not the day of the year!), so `new Date('2000-09-08') will give you August 8, not September 8, and thus making the check on the month wrong, but the end result correct, most of the time. FUN!
I will fix this.

PS: Dealing with dates is the worst, writing tests for dealing with dates is the worst worst.

gaand commented

I already have. A problem is that in ruby, a parsed date does not include a time part. In JavaScript, it always does, but then adjusts for time zone:

> new Date('1958-09-22')
// Thu Sep 07 2000 20:00:00 GMT-0400 (EDT)

This is trivially fixed using the UTC date methods:

  if (dob.getUTCMonth() > today.getUTCMonth() ||
      dob.getUTCMonth() === today.getUTCMonth() &&
      dob.getUTCDate() >= today.getUTCDate()) {
    thisYear -= 1;
  }

since this uses the timezone of the coerced date.

gaand commented

Also,

 let thisYear = today.getUTCFullYear();