SI Prefixes in JavaScript

Most people are familiar with a few of the more common prefixes used before many units to denote a fraction or a multiple of the unit - kilograms, megabytes, centimetres etc.. As well as these there a number of less well known ones, going right up to yotta and right down to yocto.

A while ago I wrote a post on SI Prefixes in Python to list all the prefixes along with their corresponding powers and multipliers. When I started writing an equivalent in JavaScript I soon hit a brick wall when I realised that JavaScript's Number type couldn't provide the precision needed.

Searching around for a solution to the problem I discovered the excellent math.js library which, amongst many other things, provides us with a BigNumber type and I thoroughly recommend reading the documentation here. This post covers my simple little JavaScript project to list the numeric SI prefixes.

Continue reading

Benford’s Law in JavaScript

Benford's Law describes the distribution of the first digits of many, if not most, sets of numeric data and in this post I will implement a demonstration of the law in JavaScript.

Benford's Law centres on the perhaps surprising fact that in numeric data such as financial transaction, populations, sizes of geographical features etc. the frequencies of first digits follow a roughly reciprocal pattern.

Continue reading

Image Histograms in JavaScript

Most cameras and image editing applications will generate histograms of image data, showing the distributions of colours for the three channels, red, green and blue. As part of an Electron application I am working on I developed a JavaScript implementation of image histograms using the NodeJS Jimp package. This is it...

Continue reading

Levenshtein Word Distance in JavaScript

In this post I'll write a JavaScript implementation of the Levenshtein Word Distance algorithm which measures the "cost" of transforming one word into another by totalling the number of letters which need to be inserted, deleted or substituted.

The Levenshtein Word Distance has a fairly obvious use in helping spell checkers decided which words to suggest as alternatives to mis-spelled words: if the distance is low between a mis-spelled word and an actual word then it is likely that word is what the user intended to type. However, it can be used in any situation where strings of characters need to be compared, such as DNA matching.

Continue reading

Sorting Algorithms in JavaScript

I recently wrote an article called Should You Learn Data Structures and Algorithms?. It is primarily about the searching and sorting algorithms which many people seem to place so much emphasis on, especially in the educational sphere. My view is that the need to implement these algorithms is rare so you would be better off concentrating on learning topics which you will need on a day to day basis. However, you might need to learn at least the basics just to convince others of your prowess so I have put together a short bit of code to illustrate a couple of the best-known sorting algorithms.

I have started of with bubble sort and selection sort. Neither are very efficient but they are simple to understand and implement. In the future I will expand this project with further algorithms.

Continue reading

Estimating Pi in JavaScript

Pi is an irrational number starting off 3.14159 and then carrying on for an infinite number of digits with no pattern which anybody has ever discovered. Therefore it cannot be calculated as such, just estimated to (in principle) any number of digits, and in this post I will implement a few of the many methods for doing so in JavaScript.

Continue reading