Add times
This commit is contained in:
19
snippets/times.md
Normal file
19
snippets/times.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
### times
|
||||||
|
|
||||||
|
Iterates over a callback `n` times
|
||||||
|
|
||||||
|
Use `Function.call()` to call `fn` `n` times or until it returns `false`.
|
||||||
|
Omit the last argument, `context`, to use an `undefined` object (or the global object in non-strict mode).
|
||||||
|
|
||||||
|
```js
|
||||||
|
const times = (n, fn, context = undefined) => {
|
||||||
|
let i = 0;
|
||||||
|
while (fn.call(context, i) !== false && ++i < n) {}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
var output = '';
|
||||||
|
times(5, i => output += i);
|
||||||
|
console.log(output); // 01234
|
||||||
|
```
|
||||||
@ -209,6 +209,7 @@ symmetricDifferenceWith:array,function
|
|||||||
tail:array
|
tail:array
|
||||||
take:array
|
take:array
|
||||||
takeRight:array
|
takeRight:array
|
||||||
|
times:function
|
||||||
timeTaken:utility
|
timeTaken:utility
|
||||||
toCamelCase:string,regexp
|
toCamelCase:string,regexp
|
||||||
toDecimalMark:utility,math
|
toDecimalMark:utility,math
|
||||||
|
|||||||
Reference in New Issue
Block a user