Get a random element from an array in JavaScript

23rd of May, 2019

TLDR: Math.floor(Math.random() * length) will give you a random index in the array, provided that length comes from array.length. The precision of this implementation over 10 million iterations is near perfect, with the biggest difference being 0.08-0.20%.

JavaScript is notorious for its tiny standard library (Standard library refers to features provided by default in the language). The lack of these functions means that you have to implement a lot of functionality yourself. This is a blessing and a curse, as you are forced to understand what is going on, but at the same time, its more time consuming, and you might end up implementing ‘bad’ (inefficient) solutions.

Some languages, like Elixir or Python, come with functionality to pick a random element from enumerable data-types (such as Arrays). In other languages, like Go or Java, you have to write the logic yourself, (sometimes) using fairly convoluted logic (Seeding random generators, etc.).

In this tiny article, I’ll quickly go through some ways to get random elements from an array in JavaScript.

Read more...

Review of 'Smashing Book #6'

14th of May, 2019

TLDR: This book has a few annoying and easily avoidable flaws, but the content is generally great. I would recommend it to developers, or even non-tech people in tech settings. 8/10.

The proper title of this book is Smashing Book #6: New Frontiers in Web Design which is a fairly fitting title; it is definitely mostly about web design, but it’s broader than that. Personally, I would have switched out ‘Web Design’ for ‘Front-end design’.

The print version of the book is 536 pages long, and is divided into 10 chapters, which can be read independently of each other. Each chapter is written by a different author, and topics vary greatly, with no overall theme beyond ‘Web Design’ (or, rather, front-end design).

Topics covered are design systems, accessibility, modern CSS features, service workers, web/browser performance, chat bots, XR (VR, AR), and uniqueness in website design. If that seems like a lot, it’s because it is.

Read more...