Shortcode fun

I’ve been using Hugo for this blog for a few years now. It works well for me and I’ve not done much customization to the theme at all. Earlier this year, I took at look Mermaid to produce some charts for inclusion in the blog. The way that worked was by using a link to content hosted on another site. That was really easy to do, but I’d rather have the content inside my blog content.

Read More

Fun with Promises

One of the problems at the end of the chapter on asynchronous programming in Modern JavaScript for the Impatient is to: Write a loop that invokes the produceRandomAfterDelay function from the preceding exercises n times and prints the sum once the summands are available. This was an interesting problem that helped me understand JavaScript promises much better. The most important learning is that promises are deferred operations and the only way to get results out of a promise is to use then.

Read More

Object literals as named objects

Some languages, like Python, have named arguments that make it easy to provide defaults and put arguments in any order. JavaScript doesn’t have this sort of thing. Modern JavaScript for the Impatient shows an interesting way to do this using object literals. The idea is that you specify your default args in a structure. That allows the function caller to override the arguments by name. If no argument value is passed in, the defaults are used.

Read More

More fun with GPT

I was curious to see how well ChatGPT could solve a simple programming problem, so I asked: Write a JavaScript function to return the prime factors of a number in an array. A few seconds later, it came back with a pretty good answer: function primeFactors(num) { const factors = []; // check if number is prime function isPrime(n) { if(n<2) return false; for (let i = 2; i <= Math.

Read More

Strictly speaking

One of the JavaScript golden rules is to use strict mode. It does tighten things up compared to the so-called “sloppy mode” and seems to generally be a good idea. Strict mode has been around for a while (since 2009) and does a bit more than just throw more errors. In the docs, the impacts of strict mode are listed out: * changes converting mistakes into errors (as syntax errors or at runtime) * changes simplifying how variable references are resolved * changes simplifying eval and arguments * changes making it easier to write "secure" JavaScript * changes anticipating future ECMAScript evolution.

Read More

There be mermaids

As part of my exploration into JavaScript, I was looking into how I might display some charts and ran across Mermaid.js. Mermaid is an interesting project that uses a simple Markdown-like language to define diagrams and charts. It’s got a long list of integrations so you can use the charts in various blogging, CMS and other tools. It’s definitely something I will be exploring more as I can see some interesting use cases for my professional work as well as hobby stuff.

Read More

Fun with Array creation

One of the problems in the chapter on Arrays and Collections in Modern JavaScript for the Impatient is Implement a function that works exactly like the from function of the Array class. Pay careful attention to missing elements. What happens with objects that have keys whose numeric values are ≥ the length property? With properties that are not index properties? This was a good way to try a few things in JavaScript.

Read More

Fun with Unicode Regex

One of the problems in the chapter on Strings and Regular Expressions in Modern JavaScript for the Impatient is Write a function that, given a string, produces an escaped string delimited by ' characters. Turn all non-ASCII Unicode into \u{. . .}. Produce escapes \b, \f, \n, \r, \t, \v, ', \. This was a pretty interesting thing to try. Working with Unicode in Regular Expressions is a little weird and it took me a while to figure out what would match non-ASCII Unicode and replace it with a encoding.

Read More

Find it with class

I recently covered the very basics of regular expression syntax. Now I’ll describe a little bit about how regular expressions work in JavaScript. The simplest method on the RegExp object is test which returns true if the string contains the specified regular expression. /[A-Z]+/.test('Foo') // true The exec method returns an array with the first match or null if there was no match. /[a-z]+.exec('foo bar') // returns Array['foo'] That array also contains the index of where the match was found (0 in this case) and the input string (‘foo bar’).

Read More

Find it

Regular expressions are a very handy and flexible way of finding string patterns. In a regex, characters are searched as they are entered except for the reserved characters: . * + ? { | ( ) [ \ ^ $ The string foo only matches the string foo. The . matches any single character so that .oo matches poo and foo. Pretty simple. The * means that the preceding character is repeated 0 or more times.

Read More