To understand how my code works, let's look a little ahead, to the beginning of February 2022.

Here is the calendar for February 2022.

```
Column# 0 1 2 3 4 5 6
Su Mo Tu We Th Fr Sa
Row 1 1 2 3 4 5
Row 2 6 7 8 9 10 11 12
Row 3 13 14 15 16 17 18 19
Row 4 20 21 22 23 24 25 26
Row 5 27 28
```

First, let us look at February 1, 2022. Look at the calendar, and find February 1 on the calendar.

Now look at this line of code (from the original program).

```
startDay = startDayOfWeek(now.year(),now.month(),1); // Sunday's value is 0
```

That line calculates the day of the week for the first day of the month, and sets `startDay`

to the result (a number from 0 to 6).

For February 2022, the first day is a Tuesday, so `startDay`

will be 2.

Now, look at these two lines I wrote:

```
int rowNumber = 1; // rows are numbered 1 to 5
int colNumber = startDay; // columns are numbered 0 to 6
```

You see, for the first day of the month, that is all we need. We are done.

"But," you ask, "what about the rest of the month?"

All right, let's get started on the rest of the month.

What about February 2 ?

Look at the calendar, and find February 2 in the calendar.

Now, look again at those two lines of code:

```
int rowNumber = 1; // rows are numbered 1 to 5
int colNumber = startDay; // columns are numbered 0 to 6
```

For February 2, the correct `rowNumber`

is still 1, so that's fine. But, what about the `colNumber`

?

From looking at our calendar, we see that the `colNumber`

value we need for February 2 is 3. But, our code doesn't set `colNumber`

to 3. It sets it to 2. (Remember, for the entire month of February 2022, `startDay`

is 2.) How do we fix this?

```
if (now.day() == 2) {
// this fixes the column number for the 2nd day of the month
colNumber = colNumber + 1;
}
```

That takes care of February 2.

Now, what about February 3 ?

Look again at the calendar grid, and find February 3.

Then, look again at my code:

```
int rowNumber = 1; // rows are numbered 1 to 5
int colNumber = startDay; // columns are numbered 0 to 6
```

OK, so `rowNumber`

is 1, which is fine.

But, `colNumber`

is not fine. It gets set to 2. But, from looking at our calendar grid, we see that we need it to be 4. How do we fix this?

```
if (now.day() == 3) {
// this fixes the column number for the 3rd day of the month
colNumber = colNumber + 2;
}
```

That takes care of February 3.

Now, what about February 4 ?

For February 4, the fix is:

```
if (now.day() == 4) {
// this fixes the column number for the 4th day of the month
colNumber = colNumber + 3;
}
```

Do you understand why this fix works for February 4 ?

To recap, we have:

```
if (now.day() == 2) {
// this fixes the column number for the 2nd day of the month
colNumber = colNumber + 1;
}
if (now.day() == 3) {
// this fixes the column number for the 3rd day of the month
colNumber = colNumber + 2;
}
if (now.day() == 4) {
// this fixes the column number for the 4th day of the month
colNumber = colNumber + 3;
}
```

Looking at this, we see a pattern.

The code for each day looks like this:

```
if (now.day() == [[number]] ) {
// this fixes the column number for the [[number]] day of the month
colNumber = colNumber + [[one less than the number]];
}
```

So, we can get rid of all of those `if`

s, and write this instead:

```
colNumber = colNumber + (now.day() - 1); // fix column number for day of month
```

or even just this:

```
colNumber += (now.day() - 1); // fix column number for day of month
```

and have a solution that will work for any day of the month, like so:

```
int rowNumber = 1; // rows are numbered 1 to 5
int colNumber = startDay; // columns are numbered 0 to 6
colNumber += (now.day() - 1); // fix column number for day of month
```

As it turns out, our solution doesn't work for *every* day of the month. The reason is, of course, that we run out of columns. We have a fix for that, too:

```
if (colNumber == 7) {
// first position past the end of the row
// so we go to the beginning of the next row
colNumber = 0;
rowNumber++;
}
if (colNumber == 8) {
// second position past the end of the row
// so we go to the second position (column 1) of the next row
colNumber = 1;
rowNumber++;
}
if (colNumber == 9) {
// third position past the end of the row
// so we go to the third position (column 2) of the next row
colNumber = 2;
rowNumber++;
}
```

Or, we can just type:

```
while (colNumber > 6) { // check if we are past the end of the row
colNumber -= 7; // move backwards 7 columns
rowNumber++; // advance to the next row
}
```

We have to be careful of moving off the end of the grid (possible in some months with 30 or 31 days). So, we take care of that like this:

```
if (rowNumber > 5) {
rowNumber = 1;
}
```

And we're done!