Build January 32 and report the month number it lands on.
Acceptance criteria
- 1.#out should show 1.
JavaScript Tutorial · JavaScript Dates
These 10 tasks go with the JavaScript Date Pitfalls chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A few date behaviours catch out almost every JavaScript developer.” .
1.See a rolloverexercise · easy · 10 min
Build January 32 and report the month number it lands on.
Passes when: #out should show 1.
2.Detect an invalid dateexercise · easy · 10 min · practice plan
Report that an unparseable string gives an invalid date.
Passes when: #out should show invalid.
3.Write a validity checkexercise · easy · 10 min · practice plan
Write a helper and use it on a good date.
Passes when: #out should show valid.
4.Protect the caller's dateexercise · easy · 10 min · practice plan
Add a week to a copy and report the original day.
Passes when: #out should show 1.
5.See February roll overexercise · medium · 10 min · practice plan
Build February 30 and report the month it lands on.
Passes when: #out should show 2.
6.Fix the invalid date left uncheckedbug fix · easy · 8 min
An invalid date gives NaN rather than throwing.
Passes when: #out should show invalid.
7.Fix the validity check using equalitybug fix · easy · 8 min · practice plan
An invalid date is never equal to NaN.
Passes when: #out should show invalid.
8.Fix the rollover assumed to throwbug fix · easy · 8 min · practice plan
An out-of-range day rolls over silently.
Passes when: #out should show 1.
9.Fix the helper changing its argumentbug fix · easy · 8 min · practice plan
The caller's date must not be modified.
Passes when: #out should show 1.
10.Fix the month passed as a human numberbug fix · easy · 8 min · practice plan
Passing 12 for December rolls into the next year.
Passes when: #out should show 11.
Between them these tasks cover Validation, Rollover and Mutability.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.