536 lines
36 KiB
JSON
536 lines
36 KiB
JSON
{
|
|
"data": [
|
|
{
|
|
"id": "binarySearch",
|
|
"title": "binarySearch",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "binarySearch.md",
|
|
"text": "Use recursion. Similar to `Array.prototype.indexOf()` that finds the index of a value within an array.\nThe 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()`.\n\nSearch a sorted array by repeatedly dividing the search interval in half.\nBegin with an interval covering the whole array.\nIf the value of the search is less than the item in the middle of the interval, recurse into the lower half. Otherwise recurse into the upper half.\nRepeatedly recurse until the value is found which is the mid or you've recursed to a point that is greater than the length which means the value doesn't exist and return `-1`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {\n if (start > end) return -1;\n const mid = Math.floor((start + end) / 2);\n if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);\n if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);\n return mid;\n};",
|
|
"es5": "var binarySearch = function binarySearch(arr, val) {\n var start = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;\n var end = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : arr.length - 1;\n if (start > end) return -1;\n var mid = Math.floor((start + end) / 2);\n if (arr[mid] > val) return binarySearch(arr, val, start, mid - 1);\n if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);\n return mid;\n};",
|
|
"example": "binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2\nbinarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1"
|
|
},
|
|
"tags": [
|
|
"algorithm",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "48d538bccbc7be7e78b8f6a69004055c4b21324d2c8d7cbc4cba0cd42e4f67fb"
|
|
}
|
|
},
|
|
{
|
|
"id": "celsiusToFahrenheit",
|
|
"title": "celsiusToFahrenheit",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "celsiusToFahrenheit.md",
|
|
"text": "Celsius to Fahrenheit temperature conversion.\n\nFollows the conversion formula `F = 1.8C + 32`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const celsiusToFahrenheit = degrees => 1.8 * degrees + 32;",
|
|
"es5": "var celsiusToFahrenheit = function celsiusToFahrenheit(degrees) {\n return 1.8 * degrees + 32;\n};",
|
|
"example": "celsiusToFahrenheit(33) // 91.4"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "d59410c4fa0ea5173af553068c1e1d4ef931695e084c50cdd8166b0cd0278722"
|
|
}
|
|
},
|
|
{
|
|
"id": "cleanObj",
|
|
"title": "cleanObj",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "cleanObj.md",
|
|
"text": "Removes any properties except the ones specified from a JSON object.\n\nUse `Object.keys()` method to loop over given JSON object and deleting keys that are not included in given array.\nIf you pass a special key,`childIndicator`, it will search deeply apply the function to inner objects, too.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const cleanObj = (obj, keysToKeep = [], childIndicator) => {\n Object.keys(obj).forEach(key => {\n if (key === childIndicator) {\n cleanObj(obj[key], keysToKeep, childIndicator);\n } else if (!keysToKeep.includes(key)) {\n delete obj[key];\n }\n });\n return obj;\n};",
|
|
"es5": "var cleanObj = function cleanObj(obj) {\n var keysToKeep = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];\n var childIndicator = arguments.length > 2 ? arguments[2] : undefined;\n Object.keys(obj).forEach(function (key) {\n if (key === childIndicator) {\n cleanObj(obj[key], keysToKeep, childIndicator);\n } else if (!keysToKeep.includes(key)) {\n delete obj[key];\n }\n });\n return obj;\n};",
|
|
"example": "const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };\ncleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}"
|
|
},
|
|
"tags": [
|
|
"object",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "aaefc9bd6e9170001fe4754b1bc7bb9808ab97a5bec7fc6ceb1193be2f8009b1"
|
|
}
|
|
},
|
|
{
|
|
"id": "collatz",
|
|
"title": "collatz",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "collatz.md",
|
|
"text": "Applies the Collatz algorithm.\n\nIf `n` is even, return `n/2`. Otherwise, return `3n+1`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);",
|
|
"es5": "var collatz = function collatz(n) {\n return n % 2 === 0 ? n / 2 : 3 * n + 1;\n};",
|
|
"example": "collatz(8); // 4"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "0280a47e49f505d5f10e0e0bd2c3ab28a6ea2b931fc83f63155f8395f64a1840"
|
|
}
|
|
},
|
|
{
|
|
"id": "countVowels",
|
|
"title": "countVowels",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "countVowels.md",
|
|
"text": "Retuns `number` of vowels in provided string.\n\nUse a regular expression to count the number of vowels `(A, E, I, O, U)` in a `string`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const countVowels = str => (str.match(/[aeiou]/gi) || []).length;",
|
|
"es5": "var countVowels = function countVowels(str) {\n return (str.match(/[aeiou]/gi) || []).length;\n};",
|
|
"example": "countVowels('foobar'); // 3\ncountVowels('gym'); // 0"
|
|
},
|
|
"tags": [
|
|
"string",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "961b406dfe98746e3a267532b0703ee7c5c1b1c01a0e04c1f9e13e0fd0aeced1"
|
|
}
|
|
},
|
|
{
|
|
"id": "factors",
|
|
"title": "factors",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "factors.md",
|
|
"text": "Returns the array of factors of the given `num`.\nIf the second argument is set to `true` returns only the prime factors of `num`.\nIf `num` is `1` or `0` returns an empty array.\nIf `num` is less than `0` returns all the factors of `-int` together with their additive inverses.\n\nUse `Array.from()`, `Array.prototype.map()` and `Array.prototype.filter()` to find all the factors of `num`.\nIf given `num` is negative, use `Array.prototype.reduce()` to add the additive inverses to the array.\nReturn all results if `primes` is `false`, else determine and return only the prime factors using `isPrime` and `Array.prototype.filter()`.\nOmit the second argument, `primes`, to return prime and non-prime factors by default.\n\n**Note**:- _Negative numbers are not considered prime._\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const factors = (num, primes = false) => {\n const isPrime = num => {\n const boundary = Math.floor(Math.sqrt(num));\n for (var i = 2; i <= boundary; i++) if (num % i === 0) return false;\n return num >= 2;\n };\n const isNeg = num < 0;\n num = isNeg ? -num : num;\n let array = Array.from({ length: num - 1 })\n .map((val, i) => (num % (i + 2) === 0 ? i + 2 : false))\n .filter(val => val);\n if (isNeg)\n array = array.reduce((acc, val) => {\n acc.push(val);\n acc.push(-val);\n return acc;\n }, []);\n return primes ? array.filter(isPrime) : array;\n};",
|
|
"es5": "var factors = function factors(num) {\n var primes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;\n\n var isPrime = function isPrime(num) {\n var boundary = Math.floor(Math.sqrt(num));\n\n for (var i = 2; i <= boundary; i++) {\n if (num % i === 0) return false;\n }\n\n return num >= 2;\n };\n\n var isNeg = num < 0;\n num = isNeg ? -num : num;\n var array = Array.from({\n length: num - 1\n }).map(function (val, i) {\n return num % (i + 2) === 0 ? i + 2 : false;\n }).filter(function (val) {\n return val;\n });\n if (isNeg) array = array.reduce(function (acc, val) {\n acc.push(val);\n acc.push(-val);\n return acc;\n }, []);\n return primes ? array.filter(isPrime) : array;\n};",
|
|
"example": "factors(12); // [2,3,4,6,12]\nfactors(12, true); // [2,3]\nfactors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]\nfactors(-12, true); // [2,3]"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "8eed39b1040d6472e2fd619abf744848d30f12eebffda2711966c616d474524f"
|
|
}
|
|
},
|
|
{
|
|
"id": "fahrenheitToCelsius",
|
|
"title": "fahrenheitToCelsius",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "fahrenheitToCelsius.md",
|
|
"text": "Fahrenheit to Celsius temperature conversion.\n\nFollows the conversion formula `C = (F - 32) * 5/9`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const fahrenheitToCelsius = degrees => (degrees - 32) * 5/9;",
|
|
"es5": "var fahrenheitToCelsius = function fahrenheitToCelsius(degrees) {\n return (degrees - 32) * 5 / 9;\n};",
|
|
"example": "fahrenheitToCelsius(32); // 0"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "a39ade2ae05ad86443446b335dbc019e3ac734fe0c1a542d50b929c554040fc0"
|
|
}
|
|
},
|
|
{
|
|
"id": "fibonacciCountUntilNum",
|
|
"title": "fibonacciCountUntilNum",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "fibonacciCountUntilNum.md",
|
|
"text": "Returns the number of fibonnacci numbers up to `num`(`0` and `num` inclusive).\n\nUse a mathematical formula to calculate the number of fibonacci numbers until `num`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const fibonacciCountUntilNum = num =>\n Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));",
|
|
"es5": "var fibonacciCountUntilNum = function fibonacciCountUntilNum(num) {\n return Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));\n};",
|
|
"example": "fibonacciCountUntilNum(10); // 7"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "35488eb0f56b59035b56cc67fa0a5e1a970162ede4aafd97ebb2b4009c321c01"
|
|
}
|
|
},
|
|
{
|
|
"id": "fibonacciUntilNum",
|
|
"title": "fibonacciUntilNum",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "fibonacciUntilNum.md",
|
|
"text": "Generates an array, containing the Fibonacci sequence, up until the nth term.\n\nCreate an empty array of the specific length, initializing the first two values (`0` and `1`).\nUse `Array.prototype.reduce()` to add values into the array, using the sum of the last two values, except for the first two.\nUses a mathematical formula to calculate the length of the array required.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const fibonacciUntilNum = num => {\n let n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));\n return Array.from({ length: n }).reduce(\n (acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),\n []\n );\n};",
|
|
"es5": "var fibonacciUntilNum = function fibonacciUntilNum(num) {\n var n = Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));\n return Array.from({\n length: n\n }).reduce(function (acc, val, i) {\n return acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i);\n }, []);\n};",
|
|
"example": "fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "6ff845c13444a06569be548ce9e69900b7001516c44c315795f34b31e9baa833"
|
|
}
|
|
},
|
|
{
|
|
"id": "heronArea",
|
|
"title": "heronArea",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "heronArea.md",
|
|
"text": "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.\n\nMore information on what Heron's formula is and why it works available here: https://en.wikipedia.org/wiki/Heron%27s_formula.\n\nUses `Math.sqrt()` to find the square root of a value.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const heronArea = (side_a, side_b, side_c) => {\n const p = (side_a + side_b + side_c) / 2\n return Math.sqrt(p * (p-side_a) * (p-side_b) * (p-side_c))\n };",
|
|
"es5": "var heronArea = function heronArea(side_a, side_b, side_c) {\n var p = (side_a + side_b + side_c) / 2;\n return Math.sqrt(p * (p - side_a) * (p - side_b) * (p - side_c));\n};",
|
|
"example": "heronArea(3, 4, 5); // 6"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "d0be594ab377cbeb2910308610af5890b3468c06e7567cd0995a84d11aaccf47"
|
|
}
|
|
},
|
|
{
|
|
"id": "howManyTimes",
|
|
"title": "howManyTimes",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "howManyTimes.md",
|
|
"text": "Returns the number of times `num` can be divided by `divisor` (integer or fractional) without getting a fractional answer.\nWorks for both negative and positive integers.\n\nIf `divisor` is `-1` or `1` return `Infinity`.\nIf `divisor` is `-0` or `0` return `0`.\nOtherwise, keep dividing `num` with `divisor` and incrementing `i`, while the result is an integer.\nReturn the number of times the loop was executed, `i`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const howManyTimes = (num, divisor) => {\n if (divisor === 1 || divisor === -1) return Infinity;\n if (divisor === 0) return 0;\n let i = 0;\n while (Number.isInteger(num / divisor)) {\n i++;\n num = num / divisor;\n }\n return i;\n};",
|
|
"es5": "var howManyTimes = function howManyTimes(num, divisor) {\n if (divisor === 1 || divisor === -1) return Infinity;\n if (divisor === 0) return 0;\n var i = 0;\n\n while (Number.isInteger(num / divisor)) {\n i++;\n num = num / divisor;\n }\n\n return i;\n};",
|
|
"example": "howManyTimes(100, 2); // 2\nhowManyTimes(100, 2.5); // 2\nhowManyTimes(100, 0); // 0\nhowManyTimes(100, -1); // Infinity"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "52ffa251dfc4e2bec7160a9066ef24a8c3047706e1ad2837f9d987cdf4d5f73e"
|
|
}
|
|
},
|
|
{
|
|
"id": "httpDelete",
|
|
"title": "httpDelete",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "httpDelete.md",
|
|
"text": "Makes a `DELETE` request to the passed URL.\n\nUse `XMLHttpRequest` web api to make a `delete` request to the given `url`.\nHandle the `onload` event, by running the provided `callback` function.\nHandle the `onerror` event, by running the provided `err` function.\nOmit the third argument, `err` to log the request to the console's error stream by default.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const httpDelete = (url, callback, err = console.error) => {\n const request = new XMLHttpRequest();\n request.open('DELETE', url, true);\n request.onload = () => callback(request);\n request.onerror = () => err(request);\n request.send();\n};",
|
|
"es5": "var httpDelete = function httpDelete(url, callback) {\n var err = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : console.error;\n var request = new XMLHttpRequest();\n request.open('DELETE', url, true);\n\n request.onload = function () {\n return callback(request);\n };\n\n request.onerror = function () {\n return err(request);\n };\n\n request.send();\n};",
|
|
"example": "httpDelete('https://website.com/users/123', request => {\n console.log(request.responseText);\n}); // 'Deletes a user from the database'"
|
|
},
|
|
"tags": [
|
|
"browser",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "4fccb2abe966313a742d13965ee46cfd1094763a2697591eddb19c1c5af1db7e"
|
|
}
|
|
},
|
|
{
|
|
"id": "httpPut",
|
|
"title": "httpPut",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "httpPut.md",
|
|
"text": "Makes a `PUT` request to the passed URL.\n\nUse `XMLHttpRequest` web api to make a `put` request to the given `url`.\nSet the value of an `HTTP` request header with `setRequestHeader` method.\nHandle the `onload` event, by running the provided `callback` function.\nHandle the `onerror` event, by running the provided `err` function.\nOmit the last argument, `err` to log the request to the console's error stream by default.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const httpPut = (url, data, callback, err = console.error) => {\n const request = new XMLHttpRequest();\n request.open(\"PUT\", url, true);\n request.setRequestHeader('Content-type','application/json; charset=utf-8');\n request.onload = () => callback(request);\n request.onerror = () => err(request);\n request.send(data);\n};",
|
|
"es5": "var httpPut = function httpPut(url, data, callback) {\n var err = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : console.error;\n var request = new XMLHttpRequest();\n request.open(\"PUT\", url, true);\n request.setRequestHeader('Content-type', 'application/json; charset=utf-8');\n\n request.onload = function () {\n return callback(request);\n };\n\n request.onerror = function () {\n return err(request);\n };\n\n request.send(data);\n};",
|
|
"example": "const password = \"fooBaz\";\nconst data = JSON.stringify(password);\nhttpPut('https://website.com/users/123', data, request => {\n console.log(request.responseText);\n}); // 'Updates a user's password in database'"
|
|
},
|
|
"tags": [
|
|
"browser",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "7eb4b1ffc1cbe28c10190bb82b7731ade2d79e78a5569bdee62af33a1020f2f5"
|
|
}
|
|
},
|
|
{
|
|
"id": "isArmstrongNumber",
|
|
"title": "isArmstrongNumber",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "isArmstrongNumber.md",
|
|
"text": "Checks if the given number is an Armstrong number or not.\n\nConvert the given number into an array of digits. Use the exponent operator (`**`) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const isArmstrongNumber = digits =>\n (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(\n (digits + '').split('')\n );",
|
|
"es5": "var isArmstrongNumber = function isArmstrongNumber(digits) {\n return function (arr) {\n return arr.reduce(function (a, d) {\n return a + Math.pow(parseInt(d), arr.length);\n }, 0) == digits;\n }((digits + '').split(''));\n};",
|
|
"example": "isArmstrongNumber(1634); // true\nisArmstrongNumber(56); // false"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "71ebcdb61794d8222fcf447509d206ffb10dc8068072a88c6b587e21e76fc7f2"
|
|
}
|
|
},
|
|
{
|
|
"id": "isSimilar",
|
|
"title": "isSimilar",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "isSimilar.md",
|
|
"text": "Determines if the `pattern` matches with `str`.\n\nUse `String.toLowerCase()` to convert both strings to lowercase, then loop through `str` and determine if it contains all characters of `pattern` and in the correct order.\nAdapted from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db53428247e741b9efe2cde9667050c/code/fts_fuzzy_match.js#L18).\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const isSimilar = (pattern, str) =>\n [...str].reduce(\n (matchIndex, char) =>\n char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()\n ? matchIndex + 1\n : matchIndex,\n 0\n ) === pattern.length;",
|
|
"es5": "function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nvar isSimilar = function isSimilar(pattern, str) {\n return _toConsumableArray(str).reduce(function (matchIndex, _char) {\n return _char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex;\n }, 0) === pattern.length;\n};",
|
|
"example": "isSimilar('rt','Rohit'); // true\nisSimilar('tr','Rohit'); // false"
|
|
},
|
|
"tags": [
|
|
"string",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "250615cfc281e99014b97d054c722d3ba6aa4190ccf66dd719e530ec80aec3bd"
|
|
}
|
|
},
|
|
{
|
|
"id": "JSONToDate",
|
|
"title": "JSONToDate",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "JSONToDate.md",
|
|
"text": "Converts a JSON object to a date.\n\nUse `Date()`, to convert dates in JSON format to readable format (`dd/mm/yyyy`).\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const JSONToDate = arr => {\n const dt = new Date(parseInt(arr.toString().substr(6)));\n return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;\n};",
|
|
"es5": "var JSONToDate = function JSONToDate(arr) {\n var dt = new Date(parseInt(arr.toString().substr(6)));\n return \"\".concat(dt.getDate(), \"/\").concat(dt.getMonth() + 1, \"/\").concat(dt.getFullYear());\n};",
|
|
"example": "JSONToDate(/Date(1489525200000)/); // \"14/3/2017\""
|
|
},
|
|
"tags": [
|
|
"object",
|
|
"date",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "33e1e304fead4088971a60d4da974d0e9380370560f383ddb1ddc14e628df18b"
|
|
}
|
|
},
|
|
{
|
|
"id": "kmphToMph",
|
|
"title": "kmphToMph",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "kmphToMph.md",
|
|
"text": "Convert kilometers/hour to miles/hour.\n\nMultiply the constant of proportionality with the argument.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const kmphToMph = (kmph) => 0.621371192 * kmph;",
|
|
"es5": "var kmphToMph = function kmphToMph(kmph) {\n return 0.621371192 * kmph;\n};",
|
|
"example": "kmphToMph(10); // 16.09344000614692\nkmphToMph(345.4); // 138.24264965280207"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "f885a599e1185f8480445e1eb0c4a5cb8bf33948d7893f013dd4e8085478fe7a"
|
|
}
|
|
},
|
|
{
|
|
"id": "levenshteinDistance",
|
|
"title": "levenshteinDistance",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "levenshteinDistance.md",
|
|
"text": "Calculates the [Levenshtein distance](https://en.wikipedia.org/wiki/Levenshtein_distance) between two strings.\n\nCalculates the number of changes (substitutions, deletions or additions) required to convert `string1` to `string2`. \nCan also be used to compare two strings as shown in the second example.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const levenshteinDistance = (string1, string2) => {\n if (string1.length === 0) return string2.length;\n if (string2.length === 0) return string1.length;\n let matrix = Array(string2.length + 1)\n .fill(0)\n .map((x, i) => [i]);\n matrix[0] = Array(string1.length + 1)\n .fill(0)\n .map((x, i) => i);\n for (let i = 1; i <= string2.length; i++) {\n for (let j = 1; j <= string1.length; j++) {\n if (string2[i - 1] === string1[j - 1]) {\n matrix[i][j] = matrix[i - 1][j - 1];\n } else {\n matrix[i][j] = Math.min(\n matrix[i - 1][j - 1] + 1,\n matrix[i][j - 1] + 1,\n matrix[i - 1][j] + 1\n );\n }\n }\n }\n return matrix[string2.length][string1.length];\n};",
|
|
"es5": "var levenshteinDistance = function levenshteinDistance(string1, string2) {\n if (string1.length === 0) return string2.length;\n if (string2.length === 0) return string1.length;\n var matrix = Array(string2.length + 1).fill(0).map(function (x, i) {\n return [i];\n });\n matrix[0] = Array(string1.length + 1).fill(0).map(function (x, i) {\n return i;\n });\n\n for (var i = 1; i <= string2.length; i++) {\n for (var j = 1; j <= string1.length; j++) {\n if (string2[i - 1] === string1[j - 1]) {\n matrix[i][j] = matrix[i - 1][j - 1];\n } else {\n matrix[i][j] = Math.min(matrix[i - 1][j - 1] + 1, matrix[i][j - 1] + 1, matrix[i - 1][j] + 1);\n }\n }\n }\n\n return matrix[string2.length][string1.length];\n};",
|
|
"example": "levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7\nconst compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));\ncompareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%)"
|
|
},
|
|
"tags": [
|
|
"algorithm",
|
|
"advanced"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "9f71509c5937cb68b65ef31893b1ad723ce6690e8ecd161707cb222ab67a475b"
|
|
}
|
|
},
|
|
{
|
|
"id": "mphToKmph",
|
|
"title": "mphToKmph",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "mphToKmph.md",
|
|
"text": "Convert miles/hour to kilometers/hour.\n\nMultiply the constant of proportionality with the argument.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const mphToKmph = (mph) => 1.6093440006146922 * mph;",
|
|
"es5": "var mphToKmph = function mphToKmph(mph) {\n return 1.6093440006146922 * mph;\n};",
|
|
"example": "mphToKmph(10); // 16.09344000614692\nmphToKmph(85.9); // 138.24264965280207"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "d1ec7968c8f8c48642a3f91878db84330231bdf84bf74633dc0754456e951338"
|
|
}
|
|
},
|
|
{
|
|
"id": "pipeLog",
|
|
"title": "pipeLog",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "pipeLog.md",
|
|
"text": "Logs a value and returns it.\n\nUse `console.log` to log the supplied value, combined with the `||` operator to return it.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const pipeLog = data => console.log(data) || data;",
|
|
"es5": "var pipeLog = function pipeLog(data) {\n return console.log(data) || data;\n};",
|
|
"example": "pipeLog(1); // logs `1` and returns `1`"
|
|
},
|
|
"tags": [
|
|
"utility",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "dba6fa36424c23d601c4e463463a5f23d32b51d8b058a6c5020d3b4098a65e51"
|
|
}
|
|
},
|
|
{
|
|
"id": "quickSort",
|
|
"title": "quickSort",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "quickSort.md",
|
|
"text": "QuickSort an Array (ascending sort by default).\n\nUse recursion.\nUse `Array.prototype.filter` and spread operator (`...`) to create an array that all elements with values less than the pivot come before the pivot, and all elements with values greater than the pivot come after it.\nIf the parameter `desc` is truthy, return array sorts in descending order.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const quickSort = ([n, ...nums], desc) =>\n isNaN(n)\n ? []\n : [\n ...quickSort(nums.filter(v => (desc ? v > n : v <= n)), desc),\n n,\n ...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)\n ];",
|
|
"es5": "function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\nfunction _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }\n\nfunction _nonIterableRest() { throw new TypeError(\"Invalid attempt to destructure non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }\n\nvar quickSort = function quickSort(_ref, desc) {\n var _ref2 = _toArray(_ref),\n n = _ref2[0],\n nums = _ref2.slice(1);\n\n return isNaN(n) ? [] : [].concat(_toConsumableArray(quickSort(nums.filter(function (v) {\n return desc ? v > n : v <= n;\n }), desc)), [n], _toConsumableArray(quickSort(nums.filter(function (v) {\n return !desc ? v > n : v <= n;\n }), desc)));\n};",
|
|
"example": "quickSort([4, 1, 3, 2]); // [1,2,3,4]\nquickSort([4, 1, 3, 2], true); // [4,3,2,1]"
|
|
},
|
|
"tags": [
|
|
"algorithm",
|
|
"recursion",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "5c26069a02342eadd2c5ba2656a1b211da8f1a94da03c2cc31a5090be556d7b7"
|
|
}
|
|
},
|
|
{
|
|
"id": "removeVowels",
|
|
"title": "removeVowels",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "removeVowels.md",
|
|
"text": "Returns all the vowels in a `str` replaced by `repl`.\n\nUse `String.prototype.replace()` with a regexp to replace all vowels in `str`.\nOmot `repl` to use a default value of `''`.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl);",
|
|
"es5": "var removeVowels = function removeVowels(str) {\n var repl = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : '';\n return str.replace(/[aeiou]/gi, repl);\n};",
|
|
"example": "removeVowels(\"foobAr\"); // \"fbr\"\nremoveVowels(\"foobAr\",\"*\"); // \"f**b*r\""
|
|
},
|
|
"tags": [
|
|
"string",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "5c6f8e292db8506568de362aa63890047f9d5a65d35143cfca1e27562642c414"
|
|
}
|
|
},
|
|
{
|
|
"id": "solveRPN",
|
|
"title": "solveRPN",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "solveRPN.md",
|
|
"text": "Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation).\nThrows 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.\n\nUse a dictionary, `OPERATORS` to specify each operator's matching mathematical operation.\nUse `String.prototype.replace()` with a regular expression to replace `^` with `**`, `String.prototype.split()` to tokenize the string and `Array.prototype.filter()` to remove empty tokens.\nUse `Array.prototype.forEach()` to parse each `symbol`, evaluate it as a numeric value or operator and solve the mathematical expression.\nNumeric values are converted to floating point numbers and pushed to a `stack`, while operators are evaluated using the `OPERATORS` dictionary and pop elements from the `stack` to apply operations.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const solveRPN = rpn => {\n const OPERATORS = {\n '*': (a, b) => a * b,\n '+': (a, b) => a + b,\n '-': (a, b) => a - b,\n '/': (a, b) => a / b,\n '**': (a, b) => a ** b\n };\n const [stack, solve] = [\n [],\n rpn\n .replace(/\\^/g, '**')\n .split(/\\s+/g)\n .filter(el => !/\\s+/.test(el) && el !== '')\n ];\n solve.forEach(symbol => {\n if (!isNaN(parseFloat(symbol)) && isFinite(symbol)) {\n stack.push(symbol);\n } else if (Object.keys(OPERATORS).includes(symbol)) {\n const [a, b] = [stack.pop(), stack.pop()];\n stack.push(OPERATORS[symbol](parseFloat(b), parseFloat(a)));\n } else {\n throw `${symbol} is not a recognized symbol`;\n }\n });\n if (stack.length === 1) return stack.pop();\n else throw `${rpn} is not a proper RPN. Please check it and try again`;\n};",
|
|
"es5": "var solveRPN = function solveRPN(rpn) {\n var OPERATORS = {\n '*': function _(a, b) {\n return a * b;\n },\n '+': function _(a, b) {\n return a + b;\n },\n '-': function _(a, b) {\n return a - b;\n },\n '/': function _(a, b) {\n return a / b;\n },\n '**': function _(a, b) {\n return Math.pow(a, b);\n }\n };\n var _ref = [[], rpn.replace(/\\^/g, '**').split(/\\s+/g).filter(function (el) {\n return !/\\s+/.test(el) && el !== '';\n })],\n stack = _ref[0],\n solve = _ref[1];\n solve.forEach(function (symbol) {\n if (!isNaN(parseFloat(symbol)) && isFinite(symbol)) {\n stack.push(symbol);\n } else if (Object.keys(OPERATORS).includes(symbol)) {\n var _ref2 = [stack.pop(), stack.pop()],\n a = _ref2[0],\n b = _ref2[1];\n stack.push(OPERATORS[symbol](parseFloat(b), parseFloat(a)));\n } else {\n throw \"\".concat(symbol, \" is not a recognized symbol\");\n }\n });\n if (stack.length === 1) return stack.pop();else throw \"\".concat(rpn, \" is not a proper RPN. Please check it and try again\");\n};",
|
|
"example": "solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5\nsolveRPN('2 3 ^'); // 8"
|
|
},
|
|
"tags": [
|
|
"algorithm",
|
|
"advanced"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "0c37cf46586652fd20dfa9ca682d3635f01fe61c46864f9773f6b258e8f3b6f6"
|
|
}
|
|
},
|
|
{
|
|
"id": "speechSynthesis",
|
|
"title": "speechSynthesis",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "speechSynthesis.md",
|
|
"text": "Performs speech synthesis (experimental).\n\nUse `SpeechSynthesisUtterance.voice` and `window.speechSynthesis.getVoices()` to convert a message to speech.\nUse `window.speechSynthesis.speak()` to play the message.\n\nLearn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const speechSynthesis = message => {\n const msg = new SpeechSynthesisUtterance(message);\n msg.voice = window.speechSynthesis.getVoices()[0];\n window.speechSynthesis.speak(msg);\n};",
|
|
"es5": "var speechSynthesis = function speechSynthesis(message) {\n var msg = new SpeechSynthesisUtterance(message);\n msg.voice = window.speechSynthesis.getVoices()[0];\n window.speechSynthesis.speak(msg);\n};",
|
|
"example": "speechSynthesis('Hello, World'); // // plays the message"
|
|
},
|
|
"tags": [
|
|
"browser",
|
|
"intermediate"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "9859dbef05dc0398e825150b50fccfea370583cf6b807c00c9e83b769d2b51c0"
|
|
}
|
|
},
|
|
{
|
|
"id": "squareSum",
|
|
"title": "squareSum",
|
|
"type": "snippet",
|
|
"attributes": {
|
|
"fileName": "squareSum.md",
|
|
"text": "Squares each number in an array and then sums the results together.\n\nUse `Array.prototype.reduce()` in combination with `Math.pow()` to iterate over numbers and sum their squares into an accumulator.\n\n",
|
|
"codeBlocks": {
|
|
"es6": "const squareSum = (...args) => args.reduce((squareSum, number) => squareSum + Math.pow(number, 2), 0);",
|
|
"es5": "var squareSum = function squareSum() {\n for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {\n args[_key] = arguments[_key];\n }\n\n return args.reduce(function (squareSum, number) {\n return squareSum + Math.pow(number, 2);\n }, 0);\n};",
|
|
"example": "squareSum(1, 2, 2); // 9"
|
|
},
|
|
"tags": [
|
|
"math",
|
|
"beginner"
|
|
]
|
|
},
|
|
"meta": {
|
|
"hash": "19837ac6714833e9c5fe698811e171cc2598f7b9405a4847b33b8d3b35debf8a"
|
|
}
|
|
}
|
|
],
|
|
"meta": {
|
|
"specification": "http://jsonapi.org/format/",
|
|
"type": "snippetArray",
|
|
"scope": "./snippets_archive"
|
|
}
|
|
} |