From f4888078d4f7c4c36ce41deb3d2c7f106f042116 Mon Sep 17 00:00:00 2001 From: 30secondsofcode <30secondsofcode@gmail.com> Date: Wed, 24 Jan 2018 11:51:58 +0000 Subject: [PATCH] Travis build: 1383 --- README.md | 29 +++++++++++++++++++++++++++++ docs/index.html | 9 ++++++++- snippets/times.md | 4 ++-- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6adbe5366..a197fc793 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,7 @@ average(1, 2, 3); * [`once`](#once) * [`runPromisesInSeries`](#runpromisesinseries) * [`sleep`](#sleep) +* [`times`](#times) @@ -3429,6 +3430,34 @@ async function sleepyWork() {
[⬆ Back to top](#table-of-contents) + +### 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) {} +}; +``` + +
+Examples + +```js +var output = ''; +times(5, i => (output += i)); +console.log(output); // 01234 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + --- ## ➗ Math diff --git a/docs/index.html b/docs/index.html index a621c02f4..4a19d1625 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -762,6 +762,13 @@ document.bodyawait sleep(1000);
   console.log('I woke up after 1 second.');
 }
+

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).

const times = (n, fn, context = undefined) => {
+  let i = 0;
+  while (fn.call(context, i) !== false && ++i < n) {}
+};
+
var output = '';
+times(5, i => (output += i));
+console.log(output); // 01234
 

Math

average

Returns the average of an of two or more numbers.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0, divide by the length of the array.

const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
 
average(...[1, 2, 3]); // 2
 average(1, 2, 3); // 2
diff --git a/snippets/times.md b/snippets/times.md
index 433cae185..2563ff5bc 100644
--- a/snippets/times.md
+++ b/snippets/times.md
@@ -9,11 +9,11 @@ Omit the last argument, `context`, to use an `undefined` object (or the global o
 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);
+times(5, i => (output += i));
 console.log(output); // 01234
 ```