Skip to content

Instantly share code, notes, and snippets.

View yavgel85's full-sized avatar

Eugene Yavgel yavgel85

View GitHub Profile
@yavgel85
yavgel85 / selectionSort.js
Created March 26, 2021 11:53
selectionSort #js #algorithm
// 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
yavgel85 / heapsort.js
Created March 26, 2021 11:52
heapsort #js #algorithm
// 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
yavgel85 / bucketSort.js
Created March 26, 2021 11:48
bucketSort #js #algorithm
// 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
yavgel85 / luhnCheck.js
Created March 26, 2021 11:47
luhnCheck #js #algorithm
// 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 Array.prototype.map() 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 + '')
.split('')
@yavgel85
yavgel85 / indexOfSubstrings.js
Created March 26, 2021 11:45
indexOfSubstrings #js #algorithm
// 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
yavgel85 / quickSort.js
Last active March 26, 2021 12:01
quickSort #js #algorithm
// 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 => {
@yavgel85
yavgel85 / isAbsoluteURL.js
Created March 26, 2021 11:35
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('https://google.com'); // true
isAbsoluteURL('ftp://www.myserver.net'); // true
isAbsoluteURL('/foo/bar'); // false
@yavgel85
yavgel85 / URLJoin.js
Created March 26, 2021 11:33
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) =>
args
.join('/')
.replace(/[\/]+/g, '/')
.replace(/^(.+):\//, '$1://')
@yavgel85
yavgel85 / getBaseURL.js
Created March 26, 2021 11:31
getBaseURL #js #browser
// 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('http://url.com/page?name=Adam&surname=Smith'); // 'http://url.com/page'
@yavgel85
yavgel85 / getURLParameters.js
Created March 26, 2021 11:29
getURLParameters #js #browser
// 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 location.search 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