## Wednesday, December 11, 2013

### How many Friday 13ths in a Year in JavaScript

Inspired by a code golf question on codegolf.stackexchange.com asking for code that counted the number of Friday 13ths in a year I came up with this inefficient NodeJS/JavaScript solution. Both the solutions below can be dropped into a .js file and run from the command line by NodeJS.

```// To run:
// node friday13.js <2013>
// <2013> is the year which you want a count of Friday 13ths in

// Add a day incrementer to the Date prototype
var value = this.valueOf();
value += 86400000 * num;
return new Date(value);
}

var year = process.argv[2];
if(!year){
console.log('Was year the first param? Received: ' + year);
return;
}

var startDate = new Date(year,0,1);
console.log(startDate);

var endDate = new Date(year,11,31);
console.log(endDate);

var counter = 0;
while(startDate <= endDate) {
if(startDate.getDay() === 5 && startDate.getDate() === 13) {
counter++;
}
}

console.log(counter + ' Friday 13ths in ' + year);
```

Someone else came up with another JavaScript solution which takes advantage of some of the idiosyncrasies of JavaScript which I thought was interesting:

```var year = process.argv[2];

var numFridays = function(year) {
var count=0;
for(month=12;month--;) {
count += !new Date(year,month,1).getDay();
}
return count;
}

console.log('Number: ' + numFridays(year));
```

Here are the interesting parts:
1. The for loop on line 5 relies on the fact that when the value of month hits zero it will evaluate to false.
2. On line 5 month will be evaluated for truthiness before it's decremented and that it will be decremented before the body of the for loop is evaluated.
3. We only have to loop through the 12 months of the year as there can only be one Friday 13th in each month so no need to go through every day.
4. The JavaScript getDay() method of the Date object returns a 0 for Sunday, 1 for Monday etc. If the month has a Friday 13th then by definition the first day of the month is a Sunday. i.e. if the value of getDay() on the 1st of the month is 0 (equivalent to false) then count this month. To do that we ! (not) the return value which gives us true which will evaluate as the value of 1 when added to an integer.

### The Turning Point to becoming a Software Engineer

I started my career as an accountant and one of my first jobs was to do the bank reconciliations (by hand) for the 56 bank accounts this manufacturing company had. Three weeks each month were spent doing this.

I asked management for a PC, a modem to connect to the banks, and access to the mainframe where the cash book was stored. Using one of the original C compilers and dBase III to store the data I pulled the statements from the bank and the mainframe. After several evenings and weekends of work I managed to automate the reconciliation process such that I could finish in a morning what had previously taken me three weeks to do. Although I had dabbled in software previously at school and university this was the turning point that I realized that I could really make a difference being a software engineer.

To give context to this I had written software in Apple BASIC and IBM/Microsoft BASIC to create trivial and contrived toy programs and to do college assignments. This was the first time that I saw the power of automation where so much time could be saved.

Since then I've worked for a number of different companies in various software development roles and for the last few years I've been managing teams of software developers. Excluding my family, the only thing I love more than solving complex problems through software is helping my team members develop and reach their full potential.

What took you into software engineering?

## Friday, December 6, 2013

### Useful Git and GitHub Commands

My notebook for Git/GitHub commands that I find useful.

To change where your local repo thinks it originally cloned itself from drop onto a command line in the appropriate directory and:
> git remote set-url origin https://github.com/<new location>
This is useful when working with a forked repo that you want to keep up-to-date with the original repo while you're working on the fork. Allows you to flip between both of them and keep changes from both current.
Follow the above command with a
> git pull
to update from the <new location>

### Useful NPM commands

My notebook for NPM commands that I find useful.

i can be used instead of install

npm -g list
- Shows everything that you've installed globally in a dependency tree graph.

npm list
- Shows everything that you've installed locally in a dependency tree graph.

## Thursday, December 5, 2013

### Understanding ES6 Fat Arrows

I'm going to try and do this explanation by manipulating a very simple line of code and transition an anonymous function to a fat arrow function.

[1,2,3].map(function(x) { return x + 1;});

Switch (swap) function and (x):

[1,2,3].map((x) function { return x + 1;});

This does not work. Just showing that you switch the word "function" with the parameter: (x)
Replace function with => (a fat arrow). You end up with this.

[1,2,3].map((x) => { return x + 1;});

Use Firefox if you want to test this as the fat arrow function hasn't been implemented in Chrome yet.

## Tuesday, December 3, 2013

### Full stack JavaScript is not about sharing code front to back

I'm watching a panel discussion at Node Summit and noticed one of the panelists saying that they found that there was very little benefit to sharing code between the front and back end when developing in a JavaScript full stack environment.

Recently I was touting the virtues of NodeJS and the person I was speaking to said "in reality how much code can you share across the stack?" I had to point out that this was not a virtue that I extolled and I've never thought of this to be a benefit of JS full stack.

In my opinion, shared code across the stack is a bonus and not a benefit. The primary benefit comes from the synergies and efficiencies in working in a single language through the stack. This means that if someone who traditionally worked on the front end has to dive in and do some work on the back end they are not going to be shocked by a different syntax and instead will be somewhat comfortable with what they see.