Algorithmic fun with JavaScript

Ransom Note

Given a magazine of words and a ransom note, determine if it's possible to “cut out” and create the ransom note from the magazine words. Enter the magazine as a list of words separated by spaces.


Sieve of Eratosthenes

For a given number, find all the prime numbers from zero to that number. Initial solutions might suffer from a time complexity worse than quadratic. Thankfully, Eratosthenes of Cyrene, the inventor of geography, also invented an efficient method for identifying prime numbers.


Fibonacci sequence

Return the Fibonacci sequence up to the desired depth.



A digit-only keyboard contains all 10 digits from 0 to 9. They all exist in one line. To type a digit, you start from index zero to the index of the target digit. It takes |a - b| milliseconds to move from index a to index b. Calculate the number of milliseconds needed to type a number with one finger.


Caesar Cipher

Given a phrase, substitute each character by shifting it up or down the alphabet by a given integer.
If necessary, the shifting should wrap around back to the beginning or end of the alphabet.


Euclidean Distance

Calculates the distance between two points in any number of dimensions. Uses Object.keys() to map each coordinate to its difference between the two points. Uses Math.hypot() to calculate the Euclidean distance between the two points.


Estimate remaining download time

A user is downloading a file which is X bytes in size. The system keeps a record of the amount (in bytes) B downloaded each minute. Calculate the remaining download time in minutes.

X - Filesize
B - Trend, listing the past bytes downloaded at each minute
Z - Last Z number of observations to be considered


Rotate Array k steps

[1, 2, 3, 4, 5] steps: 1 => [5, 1, 2, 3, 4]
[1, 2, 3, 4, 5] steps: 3 => [3, 4, 5, 1, 2]


Invert string - but only the letters

__aa**bb))cc => __cc**bb))aa
xxx-yyy&&zzz7qqq => qqq-zzz&&yyy7xxx


Number is prime

Checks if the provided integer is a prime number. By only checking numbers from 2 to the square root of the given number the complexity can be massively reduced (boundary). Returns false if any of them divides the given number, else return true, unless the number is less than 2.


Prime factors decomposition

Finds the prime factors of a given number using the trial division algorithm.
Trial division is the most laborious but easiest to understand of the integer factorization algorithms.


Factorial of Number

Calculates the factorial of a number using recursion.
Returns a notice if n is a negative number. Please don't crush the server ;-)


Classic FizzBuzz

Given a number as an input, print out every integer from 1 to that number. However, when the integer is divisible by 2, print out “Fizz”; when it's divisible by 3, print out “Buzz”; when it's divisible by both 2 and 3, print out “Fizz Buzz”.


The infamous baNaNa

Unconsidered JavaScript type casting results in hilarious results.
This example is a classic, but actually remastered from an even older original:
"foo" + +"bar" => 'fooNaN'

Output: baNaNa