Antarctic mountains

Calculating incremental increases

Here’s how to use JavaScript to calculate incremental increases over time.

Yesterday I wrote about starting the new year focusing on incremental habits that would increase over the period of a year. Here’s how I came to some calculations using JavaScript.

First, I needed to declare some constants:

const STARTING_POINT = 1;
const INCREMENT = 1;
const NUMBER_OF_WEEKS = 52;
const DAYS_PER_WEEK = 7;

Next, I create an array that has 52 values, each value indicating the week number:

let weeks = [];
for (
  let index = STARTING_POINT;
  index <= NUMBER_OF_WEEKS;
  index = index + INCREMENT
) {
  weeks.push(index);
}

Then I calculate the totals using the Array.reduce method. I’ve always been stumped at what reduce is used for, but it works well for this purpose.

const weeklyHabitTotal = weeks.reduce(
  (previousValue, currentValue) => previousValue + currentValue
);

const dailyHabitTotal = weeks.reduce(
  (previousValue, currentValue) => previousValue + currentValue * DAYS_PER_WEEK
);

Weekly habits

The weeklyHabitTotal calculates the total units of measure (e.g. minutes, pushups, potato chips eaten, etc.) that would be accumulated over a year. So suppose I wanted to ride my bike every week, I would start with one measly mile:

  • Week 1: Ride 1 mile
  • Week 2: Ride 2 miles
  • Week 52: Ride 52 miles.

At the end of a year, if I faithfully do this, I should complete 1,378 miles by the end of the year. That’s a significant improvement over the 200 miles I logged on Strava before falling in mid-2023.

Daily habits

To calculate daily habits, the only difference is that I do the habit every single day, but still increase by one every week. So if I decide to walk my dog for X number of minutes:

  • Week 1: Walk dog for 1 minute every day1.
  • Week 20: Walk dog for 20 minutes every day.
  • Week 52: Walk dog for 52 minutes.

At the end of the year, I would have walked him for 9,640 minutes or nearly 160 hours.

  1. Technically, our dog won’t let me just walk him for one minute, but it’s supposed to be a starting point. ↩︎

Discover more from AaronTweeton.com

Subscribe to get the latest posts to your email.