JavaScript Date Arithmetic — exercises and bug fixes
5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Date Arithmetic 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
Count the days between
easy10 minReport how many days separate the two dates.
- 2
Add days to a date
easy10 minAdd ten days and report the new day of the month.
- 3
Compare two dates
easy10 minReport whether the first date is earlier.
- 4
Test for the same moment
easy10 minCompare timestamps to check two dates match.
- 5
Roll into the next month
medium10 minAdd five days to January 30 and report the month number.
Find the bug(5)
- 1
Fix dates compared with triple equals
easy8 minTwo Date objects are never identical, so compare timestamps.
- 2
Fix the wrong milliseconds per day
easy8 minA day is 1000 times 60 times 60 times 24 milliseconds.
- 3
Fix the days added to the wrong part
easy8 minAdding to the month moves it far too far.
- 4
Fix the subtraction reversed
easy8 minThe later date must come first to give a positive gap.
- 5
Fix the shared date changed
easy8 minThe helper changed the caller's date instead of a copy.
Ready to write the code?
The editor, the automatic checks and the worked solutions open up with an account.
Open the workspace