Skip to main content

22 posts tagged with "javascript"

Posts about JavaScript

View All Tags

Calendar maths

· 14 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!

note

This article describes week numbering according to ISO 8601, which Sweden and most European countries follow. Other conventions exist — the United States, for example, uses Sunday as the first day of the week and treats week 1 as the week containing January 1 — so the model looks different there. The "53-week year" phenomenon explored below is specific to ISO 8601: ISO requires full seven-day weeks, while the US convention simply truncates the first and last weeks of the year to fit.

TL;DR

  • Most years have 52 weeks, but some have 53.
  • Whether a year has 53 weeks depends 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.

Sum numbers in an array with JavaScript

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

There are several ways to compute the sum of numbers in an array with JavaScript. The approaches differ mainly in how readable they are — and readable code is easier to maintain and easier for others to understand and build on.

TL;DR

  • There are many ways to sum numbers in an array, but several of them are hard to read and unnecessarily complicated.
  • The best way is using the built-in reduce function — or the newer Math.sumPrecise if you can rely on it being available.