Travis build: 176 [cron]
This commit is contained in:
@ -1460,15 +1460,12 @@ head([1, 2, 3]); // 1
|
||||
|
||||
Returns all indices of `val` in an array. If `val` never occurs, returns `[]`.
|
||||
|
||||
Use `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements.
|
||||
Use `Array.reduce()` to loop over elements and store indices for matching elements.
|
||||
Return the array of indices.
|
||||
|
||||
```js
|
||||
const indexOfAll = (arr, val) => {
|
||||
const indices = [];
|
||||
arr.forEach((el, i) => el === val && indices.push(i));
|
||||
return indices;
|
||||
};
|
||||
const indexOfAll = (arr, val) => (arr, val) =>
|
||||
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
|
||||
```
|
||||
|
||||
<details>
|
||||
|
||||
@ -187,11 +187,8 @@
|
||||
<span class="token function">groupBy</span><span class="token punctuation">([</span><span class="token string">'one'</span><span class="token punctuation">,</span> <span class="token string">'two'</span><span class="token punctuation">,</span> <span class="token string">'three'</span><span class="token punctuation">],</span> <span class="token string">'length'</span><span class="token punctuation">);</span> <span class="token comment">// {3: ['one', 'two'], 5: ['three']}</span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="head" class="section double-padded">head</h3><div class="section double-padded"><p>Returns the head of a list.</p><p>Use <code>arr[0]</code> to return the first element of the passed array.</p><pre class="language-js"><span class="token keyword">const</span> <span class="token function-variable function">head</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">];</span>
|
||||
</pre><label class="collapse">Show examples</label><pre class="language-js"><span class="token function">head</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]);</span> <span class="token comment">// 1</span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="indexofall" class="section double-padded">indexOfAll</h3><div class="section double-padded"><p>Returns all indices of <code>val</code> in an array. If <code>val</code> never occurs, returns <code>[]</code>.</p><p>Use <code>Array.forEach()</code> to loop over elements and <code>Array.push()</code> to store indices for matching elements. Return the array of indices.</p><pre class="language-js"><span class="token keyword">const</span> <span class="token function-variable function">indexOfAll</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
|
||||
<span class="token keyword">const</span> indices <span class="token operator">=</span> <span class="token punctuation">[];</span>
|
||||
arr<span class="token punctuation">.</span><span class="token function">forEach</span><span class="token punctuation">((</span>el<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> el <span class="token operator">===</span> val <span class="token operator">&&</span> indices<span class="token punctuation">.</span><span class="token function">push</span><span class="token punctuation">(</span>i<span class="token punctuation">));</span>
|
||||
<span class="token keyword">return</span> indices<span class="token punctuation">;
|
||||
};</span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="indexofall" class="section double-padded">indexOfAll</h3><div class="section double-padded"><p>Returns all indices of <code>val</code> in an array. If <code>val</code> never occurs, returns <code>[]</code>.</p><p>Use <code>Array.reduce()</code> to loop over elements and store indices for matching elements. Return the array of indices.</p><pre class="language-js"><span class="token keyword">const</span> <span class="token function-variable function">indexOfAll</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> val<span class="token punctuation">)</span> <span class="token operator">=></span>
|
||||
arr<span class="token punctuation">.</span><span class="token function">reduce</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> el<span class="token punctuation">,</span> i<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span>el <span class="token operator">===</span> val <span class="token operator">?</span> <span class="token punctuation">[</span><span class="token operator">...</span>acc<span class="token punctuation">,</span> i<span class="token punctuation">] :</span> acc<span class="token punctuation">), []);</span>
|
||||
</pre><label class="collapse">Show examples</label><pre class="language-js"><span class="token function">indexOfAll</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">1</span><span class="token punctuation">);</span> <span class="token comment">// [0,3]</span>
|
||||
<span class="token function">indexOfAll</span><span class="token punctuation">([</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">],</span> <span class="token number">4</span><span class="token punctuation">);</span> <span class="token comment">// []</span>
|
||||
</pre><button class="primary clipboard-copy">📋 Copy to clipboard</button></div></div><div class="card fluid"><h3 id="initial" class="section double-padded">initial</h3><div class="section double-padded"><p>Returns all the elements of an array except the last one.</p><p>Use <code>arr.slice(0,-1)</code> to return all but the last element of the array.</p><pre class="language-js"><span class="token keyword">const</span> <span class="token function-variable function">initial</span> <span class="token operator">=</span> arr <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">);</span>
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -1494,7 +1494,7 @@
|
||||
"fileName": "filterNonUniqueBy.md",
|
||||
"text": "Filters out the non-unique values in an array, based on a provided comparator function.\n\nUse `Array.filter()` and `Array.every()` for an array containing only the unique values, based on the comparator function, `fn`.\nThe comparator function takes four arguments: the values of the two elements being compared and their indexes.",
|
||||
"codeBlocks": [
|
||||
"const filterNonUniqueBy = (arr, fn) =>\n arr.filter((v, i) => arr.every((x, j) => (i == j) == fn(v, x, i, j)));",
|
||||
"const filterNonUniqueBy = (arr, fn) =>\n arr.filter((v, i) => arr.every((x, j) => (i === j) === fn(v, x, i, j)));",
|
||||
"filterNonUniqueBy(\n [\n { id: 0, value: 'a' },\n { id: 1, value: 'b' },\n { id: 2, value: 'c' },\n { id: 1, value: 'd' },\n { id: 0, value: 'e' }\n ],\n (a, b) => a.id == b.id\n); // [ { id: 2, value: 'c' } ]"
|
||||
],
|
||||
"tags": [
|
||||
@ -1504,7 +1504,7 @@
|
||||
},
|
||||
"meta": {
|
||||
"archived": false,
|
||||
"hash": "617e1a689baf11f30c1922cdd94bd7c3b4457aa97c60dcac81b7b2906620cbda"
|
||||
"hash": "39812240e9f09488915843a8ecb6373908513a7a469c00b120779ca462c651c5"
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -2247,9 +2247,9 @@
|
||||
"type": "snippet",
|
||||
"attributes": {
|
||||
"fileName": "indexOfAll.md",
|
||||
"text": "Returns all indices of `val` in an array. If `val` never occurs, returns `[]`.\n\nUse `Array.forEach()` to loop over elements and `Array.push()` to store indices for matching elements.\nReturn the array of indices.",
|
||||
"text": "Returns all indices of `val` in an array. If `val` never occurs, returns `[]`.\n\nUse `Array.reduce()` to loop over elements and store indices for matching elements.\nReturn the array of indices.",
|
||||
"codeBlocks": [
|
||||
"const indexOfAll = (arr, val) => {\n const indices = [];\n arr.forEach((el, i) => el === val && indices.push(i));\n return indices;\n};",
|
||||
"const indexOfAll = (arr, val) => (arr, val) =>\n arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);",
|
||||
"indexOfAll([1, 2, 3, 1, 2, 3], 1); // [0,3]\nindexOfAll([1, 2, 3], 4); // []"
|
||||
],
|
||||
"tags": [
|
||||
@ -2258,7 +2258,7 @@
|
||||
},
|
||||
"meta": {
|
||||
"archived": false,
|
||||
"hash": "e2842e376c747f02bce3cddbcbbc55ca9ed8a06701b2e0f4fb4215b97951ff13"
|
||||
"hash": "785b13b5ab3ecdeab0c40e4b4103818daf5e1ef9c2213eb19f9a41d77bfd6734"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@ -228,14 +228,14 @@
|
||||
"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.",
|
||||
"codeBlocks": [
|
||||
"``` 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).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```",
|
||||
"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": []
|
||||
},
|
||||
"meta": {
|
||||
"archived": true,
|
||||
"hash": "78b4ba08e503af63ca7811cd174218c55db9e0f726439a8c429dedeff0731d80"
|
||||
"hash": "170ef4eafeb5ac639c721f1a8e02d309e1ad62941c613ce60462fa72274f4c25"
|
||||
}
|
||||
},
|
||||
{
|
||||
@ -303,7 +303,7 @@
|
||||
},
|
||||
"meta": {
|
||||
"archived": true,
|
||||
"hash": "f654dce947e6e48d66ba5d29452c35c6031e59add0a0124ffed1239317c40ebe"
|
||||
"hash": "878970d0bdefe60f7f61db64b85bbb36b6869e2875a2c278fb269b8cff3b42b4"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@ -6,7 +6,8 @@ Use `Array.reduce()` to loop over elements and store indices for matching elemen
|
||||
Return the array of indices.
|
||||
|
||||
```js
|
||||
const indexOfAll = (arr, val) => (arr, val) => arr.reduce((acc, el, i) => el === val ? [...acc, i] : acc, []);
|
||||
const indexOfAll = (arr, val) => (arr, val) =>
|
||||
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -433,9 +433,9 @@ const levenshteinDistance = (string1, string2) => {
|
||||
if(string2.length === 0) return string1.length;
|
||||
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
|
||||
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
||||
for(let i = 1; i <= string2.length; i++){
|
||||
for(let j = 1; j<=string1.length; j++){
|
||||
if(string2[i-1] === string1[j-1]){
|
||||
for(let i = 1; i <= string2.length; i++) {
|
||||
for(let j = 1; j<=string1.length; j++) {
|
||||
if(string2[i-1] === string1[j-1]) {
|
||||
matrix[i][j] = matrix[i-1][j-1];
|
||||
}
|
||||
else{
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
const indexOfAll = (arr, val) => arr.reduce((acc, el, i) => el === val ? [...acc, i] : acc, []);
|
||||
const indexOfAll = (arr, val) => (arr, val) =>
|
||||
arr.reduce((acc, el, i) => (el === val ? [...acc, i] : acc), []);
|
||||
module.exports = indexOfAll;
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
const levenshteinDistance = (string1, string2) => {
|
||||
if(string1.length === 0) return string2.length;
|
||||
if(string2.length === 0) return string1.length;
|
||||
let matrix = Array(string2.length + 1).fill(0).map((x, i) => [i]);
|
||||
matrix[0] = Array(string1.length + 1).fill(0).map((x, i) => i);
|
||||
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
|
||||
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
|
||||
for(let i = 1; i <= string2.length; i++) {
|
||||
for(let j = 1; j<=string1.length; j++) {
|
||||
if(string2[i-1] === string1[j-1]) {
|
||||
|
||||
3191
test/testlog
3191
test/testlog
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user