Tara Roys's Blog

This WordPress.com site is the bee's knees

I want to build…

I want to build the core of my habit app, and dip a bit into HTML and javascript.

The core of the program is a single page that asks a single question: “Have you done your habit today?” If the user answers yes, the page changes to “Good work!”

Let’s implement this with just HTML and javascript.

When developing, I like to ask myself, “What is the simplest, dumbest thing I can do to get started? First, let’s build the simplest page possible.  I need to display the question and a button called “Yes” for the user to press.
<html>
<head>
</head>
<body>
<p>
Have you done your habit today?
</p>
<button> yes </button>
</body>
</html>

Right now, the code is not very interactive. When we click the button, we want the text of the question to change to “Good Work!”  Let’s add a little inline javascript.

<button onclick=”document.getElementById(‘question’).innerHTML=’Good Work’;” >

And voila!  We have a webpage that, when you click on a button, it changes the text in a box.  This is the simplest, dumbest thing I could do to get my project off the ground.

The reason I like this function is that it’s relatively readable.  Let’s walk through what this function step by step.

onclick = “document.getElementById(‘question’).innerHTML = ‘Good Work’;”

What does this function do?  It changes the text on the webpage to say Good work when you click on the button.  How does it do it?

When I read code, this is how I read that particular line.  “When the button is clicked, go to the webpage (which is named Document), find the element on it with an ID named question, get the text out of it, and replace it with ‘Good Work.’

For Javascript novices, even this function is a bit overwhelming.  More experienced Javascript coders will look at it and say “Eww, inline Javascript.”  How awful!

Yes, yes.  I said it was the simplest, dumbest thing I could write.

This is nearly the simplest, dumbest HTML page and program you can write in JavaScript.  It is also bad practice.  Next post we shall refactor it.

Habit

According to the book The Power of Habit, it takes about a month for a new habit to form.  According to Zenhabits , you should really only start one habit per month: that has the best chance for success.

I’m creating an Omega-3 supplement habit, since the right kind of omega-3 supplements have a lot of rather wondeful antidepressant effects and help your focus and concentration. I’ve done it for the last three days, and in my enthusiasm I made the mistake of trying to start a sunlight habit, a daily writing habit, and an exercize habit at the same time.   So I’m paring it down to just the omega-3 habit. I went out and bought the supplements, and I stashed them by my bed and pop pills first thing in the morning.  It takes me all of five minutes.

The rules for building habits seem absurdly simple: do something daily for a month, only focus on doing that thing, and make it as easy as possible.   It seems like the perfect test case for learning how to build a simple iphone app.

So let’s do some design.  What would our habit-forming app need to do?  In its most basic form, it would need to let you input one habit, track you for at least 30 days, and, each day, ask you if you have completed that habit. If you don’t complete the habit, you have to start back over on day one: if you do complete the habit, you win.

Lets build it.