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 card to view the snippet.

JSONToDate

Converts a JSON object to a date.

binarySearch

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().

celsiusToFahrenheit

Celsius to Fahrenheit temperature conversion.

cleanObj

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

collatz

Applies the Collatz algorithm.

countVowels

Retuns number of vowels in provided string.

factors

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.

fahrenheitToCelsius

Fahrenheit to Celsius temperature conversion.

fibonacciCountUntilNum

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

fibonacciUntilNum

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

howManyTimes

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.

httpDelete

Makes a DELETE request to the passed URL.

httpPut

Makes a PUT request to the passed URL.

isArmstrongNumber

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

isSimilar

Determines if the pattern matches with str.

kmphToMph

Convert kilometers/hour to miles/hour.

mphToKmph

Convert miles/hour to kilometers/hour.

pipeLog

Logs a value and returns it.

quickSort

QuickSort an Array (ascending sort by default).

removeVowels

Returns all the vowels in a str replaced by repl.

solveRPN

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.

speechSynthesis

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.

squareSum

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