needmoreeasy EN 한국어
Learn

33 — Habit tracker: a mark for each day

★★★★☆ (4/5)projects
Prerequisites
32 — Grade book, 12 — Random
You will end up with
keeping a yes or no for each date and counting the days it was done

A habit tracker is one answer per date, which makes it a record whose names are dates. And the dates are not typed by hand — the computer is asked for them.

Steps

  1. Open the date tools and work out today, yesterday and the day before. ago goes backwards and from now goes on:

    use date latest
    set stamp to today
    set before to yesterday
    set earlier to 2 days ago
    show stamp
    show before
    show earlier
    
    run it →
  2. Use the date as the name the answer goes under:

    use date latest
    set log to an empty record
    set stamp to today
    put stamp at yes in log
    show stamp in log
    
    run it →

    You get yes. A name in the name position means the date inside it becomes the name.

  3. Today's answer is asked for:

    use date latest
    set log to an empty record
    set stamp to today
    ask reply did you do it today
    put stamp at reply in log
    show how many log
    
    run it →
  4. Counting the days is a walk with a comparison. Take the value out and compare it:

    set log to an empty record
    put Monday at yes in log
    put Tuesday at no in log
    set done to 0
    for each day in log
        set mark to day in log
        if mark equals yes
            add 1 to done
        end
    end
    show done
    
    run it →

    You get 1.

  5. The whole thing:

    use date latest
    set log to an empty record
    set stamp to today
    set before to yesterday
    set earlier to 2 days ago
    put earlier at yes in log
    put before at no in log
    ask reply did you do it today
    put stamp at reply in log
    for each day in log
        show day
        show day in log
    end
    set done to 0
    for each day in log
        set mark to day in log
        if mark equals yes
            add 1 to done
        end
    end
    show done
    
    run it →

Try it yourself

Count the run of days in a row — walk backwards from today and break at the first day that is not yes. Then keep it in a file the way guide 36 does, so the record survives closing the program.

What you learned

  • One answer per date is a record whose names are dates.
  • yesterday and 2 days ago are the sentence way to step back; 3 days from now steps on. Only after use date latest, because that is what makes them mean a date.
  • A name in the name position puts the value inside it there instead.
  • Counting is walking, taking the value out, comparing, and adding one.

This page on GitHub