Short code snippets for all your development needs

Start of main content## JavaScript Algorithms

The JavaScript algorithms collection includes implementations and examples of popular algorithms in ES6. Algorithmic snippets are primarily a learning resource, as they might already be implemented natively or require optimizations to run in production.

### insertionSort

JavaScript, Algorithm

Sorts an array of numbers, using the insertion sort algorithm.

### mergeSort

JavaScript, Algorithm

Sorts an array of numbers, using the merge sort algorithm.

### geometricProgression

JavaScript, Math

Initializes an array containing the numbers in the specified range where

`start`

and`end`

are inclusive and the ratio between two terms is`step`

. Returns an error if`step`

equals`1`

.### powerset

JavaScript, Math

Returns the powerset of a given array of numbers.

### bubbleSort

JavaScript, Algorithm

Sorts an array of numbers, using the bubble sort algorithm.

### levenshteinDistance

JavaScript, String

Calculates the difference between two strings, using the Levenshtein distance algorithm.

### binarySearch

JavaScript, Algorithm

Finds the index of a given element in a sorted array using the binary search algorithm.

### fibonacci

JavaScript, Math

Generates an array, containing the Fibonacci sequence, up until the nth term.

### linearSearch

JavaScript, Algorithm

Finds the first index of a given element in an array using the linear search algorithm.

### primeFactors

JavaScript, Math

Finds the prime factors of a given number using the trial division algorithm.

### vectorDistance

JavaScript, Math

Calculates the distance between two vectors.

### shuffle

JavaScript, Array

Randomizes the order of the values of an array, returning a new array.

### lcm

JavaScript, Math

Calculates the least common multiple of two or more numbers.

### hammingDistance

JavaScript, Math

Calculates the Hamming distance between two values.

### binomialCoefficient

JavaScript, Math

Calculates the number of ways to choose

`k`

items from`n`

items without repetition and without order.