This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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('') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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://') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
NewerOlder