Skip to content
dreamcode
dreamcode
Projects
Habit Tracker
Guided projectJavaScript
Reward: +200 XP
PROJECT BRIEF

Habit Tracker

A habit app stores one boolean per day: true when you did the habit. Write trackHabit(days) that returns an object with three numbers:

  • total: how many days were ticked
  • longest: the longest run of ticked days in a row
  • current: the run that is still going at the end of the list
Build it step by step
  1. Loop over days with for...of.
  2. On a ticked day, add one to total and one to current.
  3. After each ticked day, update longest with Math.max(longest, current).
  4. On a missed day, reset current to 0.
Examples
trackHabit([true, true, false, true, true, true, false])
{ total: 5, longest: 3, current: 0 }
trackHabit([false, true, true])
{ total: 2, longest: 2, current: 2 }
project.js
JAVASCRIPT
Saved as you type

Tests

0 of 4 passing
  • mixed week
    trackHabit([true, true, false, true, true, true, false])
    expected { total: 5, longest: 3, current: 0 }
  • still going
    trackHabit([false, true, true])
    expected { total: 2, longest: 2, current: 2 }
  • no days
    trackHabit([])
    expected { total: 0, longest: 0, current: 0 }
  • every day
    trackHabit([true, true, true, true])
    expected { total: 4, longest: 4, current: 4 }
On the line
+200 XP
Pass all 4 tests to claim it.