Design comparison
Solution retrospective
Hello everyone, here's my solution for this challenge. I had a lot of fun building it and starting to get the hang of JavaScript, but still a long way to go. Though I got most of the logic behind the calculator, there's a an issue that I can't seem to figure out. For example someone born on the 15th of September 1977, I am getting that they would be 44years, 21 months, and 10days instead of 44years, 10months and 10 days. Your comments and feedback would be very much welcome on how I could solve this. Thank you very much.
Community feedback
- @elyyysePosted over 1 year ago
Hey, Malick - I think the error might be caused by a backwards greater than sign on line 48 of your .js file.
It’s currently written as
if (currentDate.getMonth() < birthDate.getMonth()) { ageMonths += 12; }
, but I think you’re looking for inputs where the birthday has already occurred this year.I hope this helps! I found this challenge difficult and enjoyed reading through your solution.
0@mroungouPosted over 1 year agoHi, @elyyyse thank you for your comment. I tried making the change you suggested but unfortunately it didn't seem to fix the bug :(
0@elyyysePosted over 1 year agoHi, @mroungou - thanks for reporting back! Looking back at my note, I'm the one who had the greater/less than sign flipped, not you. So sorry for the unhelpful feedback.
I took another look at your code and noticed a few things you might find helpful. Obviously any change you make will have downstream effects, but hopefully this will give you a leg up if you decide to do any refactoring.
-
Firstly, JS basically always counts from 0. So in the
Date
object,0 = January, 1 = February
, and so on. Because of this, when you are callingconst birthDate = new Date(year, month, day);
, the date you create is one month off.* (credit to this article where I learned that) -
Perhaps my favorite thing I noticed — by using UTC in
let ageYears = ageDate.getUTCFullYear() - 1970;
, you actually don't need the later 'if' statement (referenced below) to correct for users who have not yet had a birthday this year. UTC takes care of that for you. It's kind of genius, I wish I had thought of it when building my solution.
if ((currentDate.getMonth() < birthDate.getMonth()) || (currentDate.getMonth() === birthDate.getMonth() && currentDate.getDate() < birthDate.getDate())) { ageYears -- ; }
- The same is true for
let ageMonths = ageDate.getUTCMonth();
. I tested it, and it just works for telling you how many months old your user is — no adjustments necessary. (Though it's probable that in Feb/Mar during a leap year, this might act up a little). Counting days is where things get a bit tricky. I ran out of time on this one, but I can see you've already built-in adjustments to account for these issues.
* one caveat to my first bullet, if you leave off the date, so
Date(year, month, 0)
, it seems to assume you mean the previous month — so like May 0 = April 30.Anyway, that's all from me. I learned a lot about dates and times in JS by reading through your code. Seriously, using UTC like that was so smart. Cheers, Elyse
0 -
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord