Prepare repository for merge

This commit is contained in:
Angelos Chalaris
2023-05-01 22:35:56 +03:00
parent fc4e61e6fa
commit b3ad01863a
578 changed files with 0 additions and 0 deletions

View File

@ -0,0 +1,25 @@
---
title: Iterate n times
type: snippet
tags: [function]
cover: lake-loop
dateModified: 2020-10-20T11:21:07+03:00
---
Iterates over a callback `n` times.
- Use `Function.prototype.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
```