What you're building
A HabitTracker that records which days a user completed each habit and computes their current streak and longest streak. Streak logic is deceptively tricky β get it right and you understand date arithmetic and state machines.
Requirements
addHabit(name)β register a new habit.logCompletion(name, dateStr)β mark habitnameas completed ondateStr(ISO format:"2024-03-15"). Ignore duplicate log calls for the same day.getStreak(name, todayStr)β return the current streak: number of consecutive days ending on or includingtodayStrwhere the habit was completed. If today is not completed, the streak is 0.getLongestStreak(name)β return the all-time longest consecutive-day streak for the habit.getCompletedDays(name)β return a sorted array of date strings for which the habit was logged.
Stretch goals
- Add
getAllHabits()that returns each habit name with its current streak and longest streak. - Handle the case where
getStreakis called with yesterday as "today" (i.e., allow a grace day).
What this proves
You can work with date arithmetic (no library needed β just compare ISO strings and use Date arithmetic), manage sorted sets, and compute window-based aggregates β skills that appear in analytics dashboards, fitness apps, and learning platforms.
β‘ 50 XP
solution.jsRuns in your browser
Loading editorβ¦
Tests4 to pass
getStreak returns consecutive count ending today
getStreak is 0 when today is not completed
getLongestStreak finds best run
duplicate log calls do not inflate streak
Run your code to check these.