Skip to content

Instantly share code, notes, and snippets.

yavgel85 / selectionSort.js
Created Mar 26, 2021
selectionSort #js #algorithm
View selectionSort.js
// Sorts an array of numbers, using the selection sort algorithm.
// Use the spread operator (...) to clone the original array, arr.
// Use a for loop to iterate over elements in the array.
// Use Array.prototype.slice() and Array.prototype.reduce() to find the index of the minimum element in the subarray to the right of the current index and perform a swap, if necessary.
const selectionSort = arr => {
const a = [...arr];
for (let i = 0; i < a.length; i++) {
const min = a
yavgel85 / heapsort.js
Created Mar 26, 2021
heapsort #js #algorithm
View heapsort.js
// Sorts an array of numbers, using the heapsort algorithm.
// Use recursion.
// Use the spread operator (...) to clone the original array, arr.
// Use closures to declare a variable, l, and a function heapify.
// Use a for loop and Math.floor() in combination with heapify to create a max heap from the array.
// Use a for loop to repeatedly narrow down the considered range, using heapify and swapping values as necessary in order to sort the cloned array.
const heapsort = arr => {
const a = [...arr];
yavgel85 / bucketSort.js
Created Mar 26, 2021
bucketSort #js #algorithm
View bucketSort.js
// Sorts an array of numbers, using the bucket sort algorithm.
// Use Math.min(), Math.max() and the spread operator (...) to find the minimum and maximum values of the given array.
// Use Array.from() and Math.floor() to create the appropriate number of buckets (empty arrays).
// Use Array.prototype.forEach() to populate each bucket with the appropriate elements from the array.
// Use Array.prototype.reduce(), the spread operator (...) and Array.prototype.sort() to sort each bucket and append it to the result.
const bucketSort = (arr, size = 5) => {
const min = Math.min(...arr);
const max = Math.max(...arr);
yavgel85 / luhnCheck.js
Created Mar 26, 2021
luhnCheck #js #algorithm
View luhnCheck.js
// Implementation of the Luhn Algorithm used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers etc.
// Use String.prototype.split(''), Array.prototype.reverse() and in combination with parseInt() to obtain an array of digits.
// Use Array.prototype.splice(0, 1) to obtain the last digit.
// Use Array.prototype.reduce() to implement the Luhn Algorithm.
// Return true if sum is divisible by 10, false otherwise.
const luhnCheck = num => {
let arr = (num + '')
yavgel85 / indexOfSubstrings.js
Created Mar 26, 2021
indexOfSubstrings #js #algorithm
View indexOfSubstrings.js
// Finds all the indexes of a substring in a given string.
// Use Array.prototype.indexOf() to look for searchValue in str.
// Use yield to return the index if the value is found and update the index, i.
// Use a while loop that will terminate the generator as soon as the value returned from Array.prototype.indexOf() is -1.
const indexOfSubstrings = function* (str, searchValue) {
let i = 0;
while (true) {
const r = str.indexOf(searchValue, i);
yavgel85 / quickSort.js
Last active Mar 26, 2021
quickSort #js #algorithm
View quickSort.js
// Sorts an array of numbers, using the quicksort algorithm.
// Use recursion.
// Use the spread operator (...) to clone the original array, arr.
// If the length of the array is less than 2, return the cloned array.
// Use Math.floor() to calculate the index of the pivot element.
// Use Array.prototype.reduce() and Array.prototype.push() to split the array into two subarrays (elements smaller or equal to the pivot and elements greater than it), destructuring the result into two arrays.
// Recursively call quickSort() on the created subarrays.
const quickSort = arr => {
View isAbsoluteURL.js
// Checks if the given string is an absolute URL.
// Use RegExp.prototype.test() to test if the string is an absolute URL.
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
// Examples
isAbsoluteURL(''); // true
isAbsoluteURL(''); // true
isAbsoluteURL('/foo/bar'); // false
View URLJoin.js
// Joins all given URL segments together, then normalizes the resulting URL.
// Use String.prototype.join('/') to combine URL segments.
// Use a series of String.prototype.replace() calls with various regexps to normalize the resulting URL (remove double slashes, add proper slashes for protocol, remove slashes before parameters, combine parameters with '&' and normalize first parameter delimiter).
const URLJoin = (...args) =>
.replace(/[\/]+/g, '/')
.replace(/^(.+):\//, '$1://')
yavgel85 / getBaseURL.js
Created Mar 26, 2021
getBaseURL #js #browser
View getBaseURL.js
// Gets the current URL without any parameters or fragment identifiers.
// Use String.prototype.replace() with an appropriate regular expression to remove everything after either '?' or '#', if found.
const getBaseURL = url => url.replace(/[?#].*$/, '');
// Examples
getBaseURL(''); // ''
yavgel85 / getURLParameters.js
Created Mar 26, 2021
getURLParameters #js #browser
View getURLParameters.js
// Creates an object containing the parameters of the current URL.
// Use String.prototype.match() with an appropriate regular expression to get all key-value pairs.
// Use Array.prototype.reduce() to map and combine them into a single object.
// Pass as the argument to apply to the current url.
const getURLParameters = url =>
(url.match(/([^?=&]+)(=([^&]*))/g) || []).reduce(
(a, v) => (
(a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a