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

Finding Prime Numbers with the Sieve of Eratosthenes in JavaScript

Prime numbers have been understood at least since the Ancient Greeks, and possibly since the Ancient Egyptians. In modern times their study has intensified greatly due to their usefulness, notably in encryption, and because computers enable them to be calculated to a massively higher level than could be done by hand.

The best know (and according to Wikipedia still the most widely used) method for identifying them is the Sieve of Eratosthenes, which I will implement here in JavaScript.

Continue reading

The Soundex Algorithm in JavaScript

Soundex is one of a number of phonetic algorithms, assigning values to words or names so that they can be compared for similarity of pronounciation. It is probably the best know such algorithm as it is built in to most major RDBMSs, as well as PHP and other languages. Here I will write an implementation in JavaScript.

It doesn't take much thought to realise that the whole area of phonetic algorithms is a minefield, and Soundex itself is rather restricted in its usefulness. In fact, after writing this implementation I came to the conclusion that it is rather mediocre but at least coding it up does give a better understanding of how it works and therefore its usefulness and limitations.

Wikipedia has a surprisingly brief article on the topic Soundex on Wikipedia which you might like to read.

Continue reading

Great Circle Distances in JavaScript

The shortest distance between two locations on the surface of Earth (or any planet) is known as the Great Circle Distance. Except for relatively short distances these cannot be measured on a map due to the distortion and flattening necessary to represent a sphere on a flat plane. However, the calculation of the Great Circle Distance from two coordinates is simple, although I suspect generations of midshipmen might not have agreed. In this post I will write a short JavaScript application to calculate the distance from London to various other cities round the world.

Continue reading

Should You Learn Data Structures and Algorithms?

You could reasonably argue that data structures and algorithms are at the heart of computer science. This leads to the widespread belief that they are also at the heart of software development and that a detailed or even profound knowledge of them is necessary to be a decent programmer. I believe that while it doesn't do any harm to learn them in depth it is not the best use of your time.

Continue reading