Travis build: 475 [cron]
This commit is contained in:
@ -280,19 +280,19 @@
|
|||||||
"fibonacciCountUntilNum(10); // 7",
|
"fibonacciCountUntilNum(10); // 7",
|
||||||
"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};",
|
"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};",
|
||||||
"fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]",
|
"fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]",
|
||||||
"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};",
|
"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};",
|
||||||
"httpDelete('https://website.com/users/123', request => {\n console.log(request.responseText);\n}); // 'Deletes a user from the database'",
|
"httpDelete('https://website.com/users/123', request => {\n console.log(request.responseText);\n}); // 'Deletes a user from the database'",
|
||||||
"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};",
|
"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};",
|
||||||
"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'",
|
"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'",
|
||||||
"const isArmstrongNumber = digits =>\n (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(\n (digits + '').split('')\n );",
|
"const isArmstrongNumber = digits =>\n (arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(\n (digits + '').split('')\n );",
|
||||||
"isArmstrongNumber(1634); // true\nisArmstrongNumber(56); // false",
|
"isArmstrongNumber(1634); // true\nisArmstrongNumber(56); // false",
|
||||||
"const isSimilar = (pattern, str) =>\n\t[...str].reduce(\n\t\t(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0\n\t) === pattern.length ? true : false;",
|
"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\n ? true\n : false;",
|
||||||
"isSimilar('rt','Rohit'); // true\nisSimilar('tr','Rohit'); // false",
|
"isSimilar('rt','Rohit'); // true\nisSimilar('tr','Rohit'); // false",
|
||||||
"``` js\nconst 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).fill(0).map((x,i) => [i]);\n matrix[0] = Array(string1.length + 1).fill(0).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 }\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 return matrix[string2.length][string1.length];\n};\n```",
|
"``` js\nconst 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};\n```",
|
||||||
"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 (%)",
|
"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 (%)",
|
||||||
"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 ];",
|
"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 ];",
|
||||||
"quickSort([4, 1, 3, 2]); // [1,2,3,4]\nquickSort([4, 1, 3, 2], true); // [4,3,2,1]",
|
"quickSort([4, 1, 3, 2]); // [1,2,3,4]\nquickSort([4, 1, 3, 2], true); // [4,3,2,1]",
|
||||||
"const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi,repl);",
|
"const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl);",
|
||||||
"removeVowels(\"foobAr\"); // \"fbr\"\nremoveVowels(\"foobAr\",\"*\"); // \"f**b*r\"",
|
"removeVowels(\"foobAr\"); // \"fbr\"\nremoveVowels(\"foobAr\",\"*\"); // \"f**b*r\"",
|
||||||
"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};",
|
"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};",
|
||||||
"solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5\nsolveRPN('2 3 ^'); // 8",
|
"solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5\nsolveRPN('2 3 ^'); // 8",
|
||||||
@ -303,7 +303,7 @@
|
|||||||
},
|
},
|
||||||
"meta": {
|
"meta": {
|
||||||
"archived": true,
|
"archived": true,
|
||||||
"hash": "1f1d7def5d8b149626518181acb9f0e7fd0b44b31e5b42ce1e2f53e07eb01bc0"
|
"hash": "86af9032bb3fd1afc0b6e32aeca6a25c69549594804ff93f9eb0fe6e9e37236b"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||

|

|
||||||
# Snippets Archive
|
# Snippets Archive
|
||||||
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.
|
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.
|
||||||
## Table of Contents
|
## Table of Contents
|
||||||
* [`JSONToDate`](#jsontodate)
|
* [`JSONToDate`](#jsontodate)
|
||||||
* [`speechSynthesis`](#speechsynthesis)
|
* [`speechSynthesis`](#speechsynthesis)
|
||||||
* [`binarySearch`](#binarysearch)
|
* [`binarySearch`](#binarysearch)
|
||||||
@ -20,8 +20,8 @@ These snippets, while useful and interesting, didn't quite make it into the repo
|
|||||||
* [`removeVowels`](#removevowels)
|
* [`removeVowels`](#removevowels)
|
||||||
* [`solveRPN`](#solverpn)
|
* [`solveRPN`](#solverpn)
|
||||||
* [`howManyTimes`](#howmanytimes)
|
* [`howManyTimes`](#howmanytimes)
|
||||||
|
|
||||||
---
|
---
|
||||||
### JSONToDate
|
### JSONToDate
|
||||||
|
|
||||||
Converts a JSON object to a date.
|
Converts a JSON object to a date.
|
||||||
@ -33,18 +33,18 @@ const JSONToDate = arr => {
|
|||||||
const dt = new Date(parseInt(arr.toString().substr(6)));
|
const dt = new Date(parseInt(arr.toString().substr(6)));
|
||||||
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
|
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
JSONToDate(/Date(1489525200000)/); // "14/3/2017"
|
JSONToDate(/Date(1489525200000)/); // "14/3/2017"
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### speechSynthesis
|
### speechSynthesis
|
||||||
|
|
||||||
@ -61,18 +61,18 @@ const speechSynthesis = message => {
|
|||||||
msg.voice = window.speechSynthesis.getVoices()[0];
|
msg.voice = window.speechSynthesis.getVoices()[0];
|
||||||
window.speechSynthesis.speak(msg);
|
window.speechSynthesis.speak(msg);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
speechSynthesis('Hello, World'); // // plays the message
|
speechSynthesis('Hello, World'); // // plays the message
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### binarySearch
|
### binarySearch
|
||||||
|
|
||||||
@ -92,19 +92,19 @@ const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
|
|||||||
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
|
||||||
return mid;
|
return mid;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
|
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
|
||||||
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1
|
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### cleanObj
|
### cleanObj
|
||||||
|
|
||||||
@ -124,19 +124,19 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
|||||||
});
|
});
|
||||||
return obj;
|
return obj;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
|
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
|
||||||
cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
|
cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### collatz
|
### collatz
|
||||||
|
|
||||||
@ -146,18 +146,18 @@ If `n` is even, return `n/2`. Otherwise, return `3n+1`.
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);
|
const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
collatz(8); // 4
|
collatz(8); // 4
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### countVowels
|
### countVowels
|
||||||
|
|
||||||
@ -167,19 +167,19 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a `s
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
|
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
countVowels('foobar'); // 3
|
countVowels('foobar'); // 3
|
||||||
countVowels('gym'); // 0
|
countVowels('gym'); // 0
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### factors
|
### factors
|
||||||
|
|
||||||
@ -215,12 +215,12 @@ const factors = (num, primes = false) => {
|
|||||||
}, []);
|
}, []);
|
||||||
return primes ? array.filter(isPrime) : array;
|
return primes ? array.filter(isPrime) : array;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
factors(12); // [2,3,4,6,12]
|
factors(12); // [2,3,4,6,12]
|
||||||
factors(12, true); // [2,3]
|
factors(12, true); // [2,3]
|
||||||
factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]
|
factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]
|
||||||
@ -228,8 +228,8 @@ factors(-12, true); // [2,3]
|
|||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### fibonacciCountUntilNum
|
### fibonacciCountUntilNum
|
||||||
|
|
||||||
@ -240,18 +240,18 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n
|
|||||||
```js
|
```js
|
||||||
const fibonacciCountUntilNum = num =>
|
const fibonacciCountUntilNum = num =>
|
||||||
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
fibonacciCountUntilNum(10); // 7
|
fibonacciCountUntilNum(10); // 7
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### fibonacciUntilNum
|
### fibonacciUntilNum
|
||||||
|
|
||||||
@ -269,18 +269,18 @@ const fibonacciUntilNum = num => {
|
|||||||
[]
|
[]
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### httpDelete
|
### httpDelete
|
||||||
|
|
||||||
@ -294,25 +294,25 @@ Omit the third argument, `err` to log the request to the console's error stream
|
|||||||
```js
|
```js
|
||||||
const httpDelete = (url, callback, err = console.error) => {
|
const httpDelete = (url, callback, err = console.error) => {
|
||||||
const request = new XMLHttpRequest();
|
const request = new XMLHttpRequest();
|
||||||
request.open("DELETE", url, true);
|
request.open('DELETE', url, true);
|
||||||
request.onload = () => callback(request);
|
request.onload = () => callback(request);
|
||||||
request.onerror = () => err(request);
|
request.onerror = () => err(request);
|
||||||
request.send();
|
request.send();
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
httpDelete('https://website.com/users/123', request => {
|
httpDelete('https://website.com/users/123', request => {
|
||||||
console.log(request.responseText);
|
console.log(request.responseText);
|
||||||
}); // 'Deletes a user from the database'
|
}); // 'Deletes a user from the database'
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### httpPut
|
### httpPut
|
||||||
|
|
||||||
@ -326,19 +326,19 @@ Omit the last argument, `err` to log the request to the console's error stream b
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const httpPut = (url, data, callback, err = console.error) => {
|
const httpPut = (url, data, callback, err = console.error) => {
|
||||||
const request = new XMLHttpRequest();
|
const request = new XMLHttpRequest();
|
||||||
request.open("PUT", url, true);
|
request.open("PUT", url, true);
|
||||||
request.setRequestHeader('Content-type','application/json; charset=utf-8');
|
request.setRequestHeader('Content-type','application/json; charset=utf-8');
|
||||||
request.onload = () => callback(request);
|
request.onload = () => callback(request);
|
||||||
request.onerror = () => err(request);
|
request.onerror = () => err(request);
|
||||||
request.send(data);
|
request.send(data);
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const password = "fooBaz";
|
const password = "fooBaz";
|
||||||
const data = JSON.stringify(password);
|
const data = JSON.stringify(password);
|
||||||
httpPut('https://website.com/users/123', data, request => {
|
httpPut('https://website.com/users/123', data, request => {
|
||||||
@ -347,8 +347,8 @@ httpPut('https://website.com/users/123', data, request => {
|
|||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### isArmstrongNumber
|
### isArmstrongNumber
|
||||||
|
|
||||||
@ -361,19 +361,19 @@ const isArmstrongNumber = digits =>
|
|||||||
(arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
|
(arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
|
||||||
(digits + '').split('')
|
(digits + '').split('')
|
||||||
);
|
);
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
isArmstrongNumber(1634); // true
|
isArmstrongNumber(1634); // true
|
||||||
isArmstrongNumber(56); // false
|
isArmstrongNumber(56); // false
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### isSimilar
|
### isSimilar
|
||||||
|
|
||||||
@ -384,22 +384,28 @@ Adapted from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db5
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const isSimilar = (pattern, str) =>
|
const isSimilar = (pattern, str) =>
|
||||||
[...str].reduce(
|
[...str].reduce(
|
||||||
(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
|
(matchIndex, char) =>
|
||||||
) === pattern.length ? true : false;
|
char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()
|
||||||
```
|
? matchIndex + 1
|
||||||
|
: matchIndex,
|
||||||
|
0
|
||||||
|
) === pattern.length
|
||||||
|
? true
|
||||||
|
: false;
|
||||||
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
isSimilar('rt','Rohit'); // true
|
isSimilar('rt','Rohit'); // true
|
||||||
isSimilar('tr','Rohit'); // false
|
isSimilar('tr','Rohit'); // false
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### levenshteinDistance
|
### levenshteinDistance
|
||||||
|
|
||||||
@ -410,36 +416,43 @@ Can also be used to compare two strings as shown in the second example.
|
|||||||
|
|
||||||
``` js
|
``` js
|
||||||
const levenshteinDistance = (string1, string2) => {
|
const levenshteinDistance = (string1, string2) => {
|
||||||
if(string1.length === 0) return string2.length;
|
if (string1.length === 0) return string2.length;
|
||||||
if(string2.length === 0) return string1.length;
|
if (string2.length === 0) return string1.length;
|
||||||
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
|
let matrix = Array(string2.length + 1)
|
||||||
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
.fill(0)
|
||||||
for(let i = 1; i <= string2.length; i++) {
|
.map((x, i) => [i]);
|
||||||
for(let j = 1; j<=string1.length; j++) {
|
matrix[0] = Array(string1.length + 1)
|
||||||
if(string2[i-1] === string1[j-1]) {
|
.fill(0)
|
||||||
matrix[i][j] = matrix[i-1][j-1];
|
.map((x, i) => i);
|
||||||
}
|
for (let i = 1; i <= string2.length; i++) {
|
||||||
else{
|
for (let j = 1; j <= string1.length; j++) {
|
||||||
matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1);
|
if (string2[i - 1] === string1[j - 1]) {
|
||||||
}
|
matrix[i][j] = matrix[i - 1][j - 1];
|
||||||
}
|
} else {
|
||||||
|
matrix[i][j] = Math.min(
|
||||||
|
matrix[i - 1][j - 1] + 1,
|
||||||
|
matrix[i][j - 1] + 1,
|
||||||
|
matrix[i - 1][j] + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return matrix[string2.length][string1.length];
|
}
|
||||||
|
return matrix[string2.length][string1.length];
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7
|
levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7
|
||||||
const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));
|
const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));
|
||||||
compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%)
|
compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%)
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### quickSort
|
### quickSort
|
||||||
|
|
||||||
@ -458,19 +471,19 @@ const quickSort = ([n, ...nums], desc) =>
|
|||||||
n,
|
n,
|
||||||
...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
|
...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
|
||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
quickSort([4, 1, 3, 2]); // [1,2,3,4]
|
quickSort([4, 1, 3, 2]); // [1,2,3,4]
|
||||||
quickSort([4, 1, 3, 2], true); // [4,3,2,1]
|
quickSort([4, 1, 3, 2], true); // [4,3,2,1]
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### removeVowels
|
### removeVowels
|
||||||
|
|
||||||
@ -480,20 +493,20 @@ Use `String.replace()` with a regexp to replace all vowels in `str`.
|
|||||||
Omot `repl` to use a default value of `''`.
|
Omot `repl` to use a default value of `''`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi,repl);
|
const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl);
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
removeVowels("foobAr"); // "fbr"
|
removeVowels("foobAr"); // "fbr"
|
||||||
removeVowels("foobAr","*"); // "f**b*r"
|
removeVowels("foobAr","*"); // "f**b*r"
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### solveRPN
|
### solveRPN
|
||||||
|
|
||||||
@ -534,19 +547,19 @@ const solveRPN = rpn => {
|
|||||||
if (stack.length === 1) return stack.pop();
|
if (stack.length === 1) return stack.pop();
|
||||||
else throw `${rpn} is not a proper RPN. Please check it and try again`;
|
else throw `${rpn} is not a proper RPN. Please check it and try again`;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5
|
solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5
|
||||||
solveRPN('2 3 ^'); // 8
|
solveRPN('2 3 ^'); // 8
|
||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
### howManyTimes
|
### howManyTimes
|
||||||
|
|
||||||
@ -569,12 +582,12 @@ const howManyTimes = (num, divisor) => {
|
|||||||
}
|
}
|
||||||
return i;
|
return i;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
<details>
|
<details>
|
||||||
<summary>Examples</summary>
|
<summary>Examples</summary>
|
||||||
|
|
||||||
```js
|
```js
|
||||||
howManyTimes(100, 2); // 2
|
howManyTimes(100, 2); // 2
|
||||||
howManyTimes(100, 2.5); // 2
|
howManyTimes(100, 2.5); // 2
|
||||||
howManyTimes(100, 0); // 0
|
howManyTimes(100, 0); // 0
|
||||||
@ -582,6 +595,6 @@ howManyTimes(100, -1); // Infinity
|
|||||||
```
|
```
|
||||||
|
|
||||||
</details>
|
</details>
|
||||||
|
|
||||||
[⬆ Back to top](#table-of-contents)
|
<br>[⬆ Back to top](#table-of-contents)
|
||||||
|
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
const observeMutations = (element, callback, options) => {
|
const observeMutations = (element, callback, options) => {
|
||||||
const observer = new MutationObserver(mutations => mutations.forEach(callback));
|
const observer = new MutationObserver(mutations => mutations.forEach(m => callback(m)));
|
||||||
observer.observe(
|
observer.observe(
|
||||||
element,
|
element,
|
||||||
Object.assign(
|
Object.assign(
|
||||||
|
|||||||
3496
test/testlog
3496
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user