Travis build: 568

This commit is contained in:
30secondsofcode
2018-10-01 16:35:03 +00:00
parent a950d6a565
commit 77dfa88310
3 changed files with 7 additions and 13 deletions

View File

@ -2740,15 +2740,11 @@ takeRight([1, 2, 3]); // [3]
Removes elements from the end of an array until the passed function returns `true`. Returns the removed elements.
Loop through the array, using a `for...of` loop over `Array.prototype.keys()` until the returned value from the function is `true`.
Return the removed elements, using `Array.prototype.reverse()` and `Array.prototype.slice()`.
Loop through the array, using a `Array.prototype.reduceRight()` and accumulating elements while the function returns falsy value.
```js
const takeRightWhile = (arr, func) => {
for (let i of arr.reverse().keys())
if (func(arr[i])) return arr.reverse().slice(arr.length - i, arr.length);
return arr;
};
const takeRightWhile = (arr, func) =>
arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
```
<details>

View File

@ -513,11 +513,8 @@ console<span class="token punctuation">.</span><span class="token function">log<
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takeright">takeRight</h4><p>Returns an array with n elements removed from the end.</p><p>Use <code>Array.prototype.slice()</code> to create a slice of the array with <code>n</code> elements taken from the end.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeRight</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> n <span class="token operator">=</span> <span class="token number">1</span><span class="token punctuation">)</span> <span class="token operator">=></span> arr<span class="token punctuation">.</span><span class="token function">slice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> n<span class="token punctuation">,</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">takeRight</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">2</span><span class="token punctuation">);</span> <span class="token comment">// [ 2, 3 ]</span>
<span class="token function">takeRight</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">// [3]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takerightwhile">takeRightWhile</h4><p>Removes elements from the end of an array until the passed function returns <code>true</code>. Returns the removed elements.</p><p>Loop through the array, using a <code>for...of</code> loop over <code>Array.prototype.keys()</code> until the returned value from the function is <code>true</code>. Return the removed elements, using <code>Array.prototype.reverse()</code> and <code>Array.prototype.slice()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeRightWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">let</span> i <span class="token keyword">of</span> arr<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">().</span><span class="token function">keys</span><span class="token punctuation">())</span>
<span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>arr<span class="token punctuation">[</span>i<span class="token punctuation">]))</span> <span class="token keyword">return</span> arr<span class="token punctuation">.</span><span class="token function">reverse</span><span class="token punctuation">().</span><span class="token function">slice</span><span class="token punctuation">(</span>arr<span class="token punctuation">.</span>length <span class="token operator">-</span> i<span class="token punctuation">,</span> arr<span class="token punctuation">.</span>length<span class="token punctuation">);</span>
<span class="token keyword">return</span> arr<span class="token punctuation">;
};</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takerightwhile">takeRightWhile</h4><p>Removes elements from the end of an array until the passed function returns <code>true</code>. Returns the removed elements.</p><p>Loop through the array, using a <code>Array.prototype.reduceRight()</code> and accumulating elements while the function returns falsy value.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeRightWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span>
arr<span class="token punctuation">.</span><span class="token function">reduceRight</span><span class="token punctuation">((</span>acc<span class="token punctuation">,</span> el<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>el<span class="token punctuation">)</span> <span class="token operator">?</span> acc <span class="token punctuation">: [</span>el<span class="token punctuation">,</span> <span class="token operator">...</span>acc<span class="token punctuation">]), []);</span>
</pre><label class="collapse">examples</label><pre class="section card-examples language-js"><span class="token function">takeRightWhile</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> n <span class="token operator">=></span> n <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">);</span> <span class="token comment">// [3, 4]</span>
</pre></div><div class="card code-card"><div class="corner intermediate"></div><div class="section card-content"><h4 id="takewhile">takeWhile</h4><p>Removes elements in an array until the passed function returns <code>true</code>. Returns the removed elements.</p><p>Loop through the array, using a <code>for...of</code> loop over <code>Array.prototype.entries()</code> until the returned value from the function is <code>true</code>. Return the removed elements, using <code>Array.prototype.slice()</code>.</p></div><div class="copy-button-container"><button class="copy-button" aria-label="Copy to clipboard"></button></div><pre class="section card-code language-js"><span class="token keyword">const</span> <span class="token function-variable function">takeWhile</span> <span class="token operator">=</span> <span class="token punctuation">(</span>arr<span class="token punctuation">,</span> func<span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token punctuation">{</span>
<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token punctuation">[</span>i<span class="token punctuation">,</span> val<span class="token punctuation">]</span> <span class="token keyword">of</span> arr<span class="token punctuation">.</span><span class="token function">entries</span><span class="token punctuation">())</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token function">func</span><span class="token punctuation">(</span>val<span class="token punctuation">))</span> <span class="token keyword">return</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> i<span class="token punctuation">);</span>

View File

@ -5,7 +5,8 @@ Removes elements from the end of an array until the passed function returns `tru
Loop through the array, using a `Array.prototype.reduceRight()` and accumulating elements while the function returns falsy value.
```js
const takeRightWhile = (arr, func) => arr.reduceRight((acc, el) => func(el) ? acc : [el, ...acc], []);
const takeRightWhile = (arr, func) =>
arr.reduceRight((acc, el) => (func(el) ? acc : [el, ...acc]), []);
```
```js