30 seconds of code

Archived snippets

These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.

Click on a snippet's name to view its code.


Converts a JSON object to a date.


Use recursion. Similar to Array.prototype.indexOf() that finds the index of a value within an array. The difference being this operation only works with sorted arrays which offers a major performance boost due to it's logarithmic nature when compared to a linear search or Array.prototype.indexOf().


Celsius to Fahrenheit temperature conversion.


Removes any properties except the ones specified from a JSON object.


Applies the Collatz algorithm.


Retuns number of vowels in provided string.


Returns the array of factors of the given num. If the second argument is set to true returns only the prime factors of num. If num is 1 or 0 returns an empty array. If num is less than 0 returns all the factors of -int together with their additive inverses.

Use Array.from(), Array.prototype.map() and Array.prototype.filter() to find all the factors of num. If given num is negative, use Array.prototype.reduce() to add the additive inverses to the array. Return all results if primes is false, else determine and return only the prime factors using isPrime and Array.prototype.filter(). Omit the second argument, primes, to return prime and non-prime factors by default.


Fahrenheit to Celsius temperature conversion.


Returns the number of fibonnacci numbers up to num(0 and num inclusive).


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


Returns the area of a triangle using only the 3 side lengths, Heron's formula. Assumes that the sides define a valid triangle. Does NOT assume it is a right triangle.

More information on what Heron's formula is and why it works available here: https://en.wikipedia.org/wiki/Heron%27s_formula.


Returns the number of times num can be divided by divisor (integer or fractional) without getting a fractional answer. Works for both negative and positive integers.


Makes a DELETE request to the passed URL.


Makes a PUT request to the passed URL.


Checks if the given number is an Armstrong number or not.


Determines if the pattern matches with str.


Convert kilometers/hour to miles/hour.


Calculates the Levenshtein distance between two strings.


Convert miles/hour to kilometers/hour.


Logs a value and returns it.


QuickSort an Array (ascending sort by default).


Returns all the vowels in a str replaced by repl.


Solves the given mathematical expression in reverse polish notation. Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- +,-,*,/,^,** (^&** are the exponential symbols and are same). This snippet does not supports any unary operators.


Performs speech synthesis (experimental).

Use SpeechSynthesisUtterance.voice and window.speechSynthesis.getVoices() to convert a message to speech. Use window.speechSynthesis.speak() to play the message.


Squares each number in an array and then sums the results together.