Weeknotes for 2nd December 2019 – 8th December 2019

In an effort to keep my blogging momentum up, here’s my third consecutive #weeknotes post. As in past weeks, I’ll be rounding up any of the interesting things I’ve been searching for on the internet.

πŸ”Ž advent of code

For the past few years, every December, Eric Wastl has run a series of coding challenges called Advent of Code. These work like an Advent Calendar, with a new challenge becoming available each day. They can be solved using any tools or programming languages you want, so I have chosen NodeJS & regular JS to keep the tooling to a minimum. I started off strongly, but I’ve not had enough time to dedicate to it yet and I’ve started running behind. To keep an eye on my progess, you can follow my GitHub repo, here.

πŸ”Ž mdn reduceΒ &&Β πŸ”Ž mdn biggest number

One of the Advent of Code challenges had us searching for the smallest number in a list of results. My favorite aspect of programming in JavaScript over many other languages is it’s ability to work with lists and arrays, gained from its functional programming roots. This lets us process arrays using concepts like map and reduce giving us the following snippet for retrieving the smallest item from a list.

console.log([1, 2, 3, 4, 5].reduce(
    (prev, curr) => Math.min(prev, curr),
    Number.MAX_VALUE));Code language: JavaScript (javascript)

I used Number.MAX_VALUE as the initalValue in the reduce call as that’s the largest number JavaScript can hold; any other given value must be smaller than it, making it an ideal starting point for finding minimums in a list. Number.POSITIVE_INFINITY is also available, and could even be a better choice due to the Number.isFinite() method to make sure we’ve actually found a smaller number.

πŸ”Ž mdn spread operator array copy

When I was prototyping an answer for another challenge, I found myself needing to create a copy of an array, not just a reference to it, and the spread operator is great for that. It creates a new array with all of the original array members contained within.

const testArray1 = [1, 2, 3, 4, 5];

const testArray2 = testArray1;
testArray2.push('Everywhere');

const testArray3 = [...testArray1];
testArray3.push('Just Here');

console.log(testArray1); // [ 1, 2, 3, 4, 5, 'Everywhere' ]
console.log(testArray2); // [ 1, 2, 3, 4, 5, 'Everywhere' ]
console.log(testArray3); // [ 1, 2, 3, 4, 5, 'Everywhere', 'Just Here' ]Code language: JavaScript (javascript)

πŸ”Ž git change old commit messages

Working as a full-stack developer often means fixing bugs or adding features that cross both the front-end and back-end sytems. At work, we follow Chris Beams’ seven rules for git commit messages, which means ending our messages with references to the issues that the piece of work was for. Jumping between different repos for front and back-ends can result in the front-end commits being tagged with the back-end issues and vice-versa. To fix this, and re-write a series of commit messages, you can take advantage of git’s interactive rebase mode, and rewrite the last, say, 3 messages by using the following command.

$ git rebase -i HEAD~3Code language: Shell Session (shell)

You will then be presented with a summary listing those commits.

pick 55f9d3e Advent of Code 2018, Day 1, Stars 1 & 2
pick d577864 Advent of Code 2019, Day 2, Star 1
pick 62de55e Advent of Code 2019, Day 2, Star 2Code language: Shell Session (shell)

Changing each of those picks (which would just use the commit as-is) to rewords tells git to accept the code changes, but re-write the commit messages, allowing you to fix your incorrect issue references.

reword 55f9d3e Advent of Code 2018, Day 1, Stars 1 & 2
reword d577864 Advent of Code 2019, Day 2, Star 1
reword 62de55e Advent of Code 2019, Day 2, Star 2Code language: Shell Session (shell)

Posted

in

by

Tags:

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *