Skip to main content

21 posts tagged with "javascript"

View All Tags

Operations in JavaScript with immutable arrays

· 3 min read
Filip Tammergård
Software Engineer at Frilans Finans

As mentioned many times before, I always try to keep my variables immutable when developing. Let's take a closer look at how we accomplish immutable arrays in JavaScript when adding, replacing or removing elements. We start with an array of birds:

const birds = ["Great tit", "Blue tit", "Pine grosbeak", "Stock dove"]

Format lists with Intl.ListFormat

· 3 min read
Filip Tammergård
Software Engineer at Frilans Finans

Let's say we have this array of cities:

const cities = ["Uppsala", "Stockholm", "Lycksele", "Lund"]

And let's say we want to write the cities from the array in a sentence like this:

Uppsala, Stockholm, Lycksele and Lund are 4 nice cities in Sweden.

How?!

Flatten array and count occurrences

· 4 min read
Filip Tammergård
Software Engineer at Frilans Finans

I recently added support for filtering blog posts by categories. (I've since replaced that with category and author pages, so this functionality is no longer used.) Next to each category in the filter view, the number of posts in that category was also shown. In this post I'll walk through what I was trying to accomplish and how I solved it.

Alphabet game

· One min read
Filip Tammergård
Software Engineer at Frilans Finans

In this game, you're supposed to write the alphabet as fast as you can. Click the input box and start the game by typing the first letter of the alphabet. My highscore is 2.95 seconds. Beat that if you can!

Birthday maths

· 9 min read
Filip Tammergård
Software Engineer at Frilans Finans

Have you ever wondered how old you are…in milliseconds? Or exactly how many weeks there are between two dates? Don't worry, in this post you can get the answer to all your "How many [unit] is it between [date] and [date]?" questions!

Robber language generator

· 8 min read
Filip Tammergård
Software Engineer at Frilans Finans

Robber language (Swedish: rövarspråket) is said to have been invented by Sture Lindgren — husband of the author Astrid Lindgren — when he played with his friends as a child. Astrid Lindgren's books about Kalle Blomkvist made the language popular in Sweden. Robber language is a spoken code where every consonant is replaced by the consonant + "o" + the consonant again. From that simple rule, building a robber language generator should be dead easy. When it comes to written robber language, however, there are a few more aspects to consider. So this post is really about a written robber language generator.

TL;DR

  • Robber language has a simple rule for spoken language, but a few rules need to be added to make the written form match the spoken rules and standard writing conventions.
  • A robber language generator can be built in JavaScript in many ways. Two main strategies I walk through in this post are starting from the consonants of the alphabet, and starting from the sentence that should be translated.

Try the finished robber language generator here:

Calendar maths

· 12 min read
Filip Tammergård
Software Engineer at Frilans Finans

You might think week numbers are dead simple. Surely you just start at week 1 and go up to week 52, then start over? Unfortunately it's not that easy. It turns out to be surprisingly complicated!

TL;DR

  • Most years have 52 weeks, but some have 53.
  • Whether the 53rd week exists depends both on what day of the week January 1 of the following year falls on, and whether the current year is a leap year.
  • Leap years occur slightly less than every four years on average, which makes the model for when 53 weeks appear tricky.

Try the finished week-numbering and leap-year model here: