Skip to main content

JavaScript Date Pitfalls — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Date Pitfalls lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    See a rollover

    easy10 min

    Build January 32 and report the month number it lands on.

  2. 2

    Detect an invalid date

    easy10 min

    Report that an unparseable string gives an invalid date.

  3. 3

    Write a validity check

    easy10 min

    Write a helper and use it on a good date.

  4. 4

    Protect the caller's date

    easy10 min

    Add a week to a copy and report the original day.

  5. 5

    See February roll over

    medium10 min

    Build February 30 and report the month it lands on.

Find the bug(5)

  1. 1

    Fix the invalid date left unchecked

    easy8 min

    An invalid date gives NaN rather than throwing.

  2. 2

    Fix the validity check using equality

    easy8 min

    An invalid date is never equal to NaN.

  3. 3

    Fix the rollover assumed to throw

    easy8 min

    An out-of-range day rolls over silently.

  4. 4

    Fix the helper changing its argument

    easy8 min

    The caller's date must not be modified.

  5. 5

    Fix the month passed as a human number

    easy8 min

    Passing 12 for December rolls into the next year.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace