Promise.resolve([1,2,3])
.then(call('map', x =>2* x))
.then(console.log);//[ 2, 4, 6 ]
@@ -762,6 +762,13 @@ document.bodyawaitsleep(1000);
console.log('I woke up after 1 second.');
}
+
times
Iterates over a callback n times
Use Function.call() to call fnn times or until it returns false. Omit the last argument, context, to use an undefined object (or the global object in non-strict mode).
consttimes=(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.
average(...[1,2,3]);// 2average(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
```