From b675aba398ce777b6caa06ad2984e770d1406707 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Mon, 1 Jan 2018 14:30:42 +0000 Subject: [PATCH] Travis build: 784 [ci skip] --- README.md | 36 ++++++++++++++++++++++++++++++++++-- docs/index.html | 11 ++++++++++- snippets/isArrayLike.md | 1 + snippets/sumPower.md | 11 +++++++---- 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d302b8268..84a17213f 100644 --- a/README.md +++ b/README.md @@ -177,6 +177,7 @@ * [`round`](#round) * [`standardDeviation`](#standarddeviation) * [`sum`](#sum) +* [`sumPower`](#sumpower) @@ -237,7 +238,7 @@ -### 💎 Utility +### 🔧 Utility
View contents @@ -3094,6 +3095,36 @@ sum([1, 2, 3, 4]); // 10
[⬆ Back to top](#table-of-contents) + +### sumPower + +Returns the sum of the powers of all the numbers from `start` to `end` (both inclusive). + +Use `Array.fill()` to create an array of all the numbers in the target range, `Array.map()` and the exponent operator (`**`) to raise them to `power` and `Array.reduce()` to add them together. +Omit the second argument, `power`, to use a default power of `2`. +Omit the third argument, `start`, to use a default starting value of `1`. + +```js +const sumPower = (end, power = 2, start = 1) => + Array(end + 1 - start) + .fill(0) + .map((x, i) => (i + start) ** power) + .reduce((a, b) => a + b, 0); +``` + +
+Examples + +```js +sumPower(10); // 385 +sumPower(10, 3); //3025 +sumPower(10, 3, 5); //2925 +``` + +
+ +
[⬆ Back to top](#table-of-contents) + --- ## 📦 Node @@ -4012,7 +4043,7 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
[⬆ Back to top](#table-of-contents) --- - ## 💎 Utility + ## 🔧 Utility ### coalesce @@ -4211,6 +4242,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in ```js + const isArrayLike = val => try {return [...val], true; } catch (e) { return false; } diff --git a/docs/index.html b/docs/index.html index 4f5e25ed6..2fb24395e 100644 --- a/docs/index.html +++ b/docs/index.html @@ -59,7 +59,7 @@ wrapper.appendChild(box); box.appendChild(el); }); - }

 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);
+    }

 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 ]
@@ -612,6 +612,14 @@ median([0, 10, -2, 7]); // 3.5
 standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (population)
 

sum

Returns the sum of an of two or more numbers/arrays.

Use Array.reduce() to add each value to an accumulator, initialized with a value of 0.

const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
 
sum([1, 2, 3, 4]); // 10
+

sumPower

Returns the sum of the powers of all the numbers from start to end (both inclusive).

Use Array.fill() to create an array of all the numbers in the target range, Array.map() and the exponent operator (**) to raise them to power and Array.reduce() to add them together. Omit the second argument, power, to use a default power of 2. Omit the third argument, start, to use a default starting value of 1.

const sumPower = (end, power = 2, start = 1) =>
+  Array(end + 1 - start)
+    .fill(0)
+    .map((x, i) => (i + start) ** power)
+    .reduce((a, b) => a + b, 0);
+
sumPower(10); // 385
+sumPower(10, 3); //3025
+sumPower(10, 3, 5); //2925
 

Node

JSONToFile

Writes a JSON object to a file.

Use fs.writeFile(), template literals and JSON.stringify() to write a json object to a .json file.

const fs = require('fs');
 const JSONToFile = (obj, filename) =>
   fs.writeFile(`${filename}.json`, JSON.stringify(obj, null, 2));
@@ -887,6 +895,7 @@ hexToRGB('#fff'); // 'rgb(255, 255, 255)'
 isArray([1]); // true
 

isArrayLike

Checks if the provided argument is array-like (i.e. is iterable).

Use the spread operator (...) to check if the provided argument is iterable inside a try... catch block and the comma operator (,) to return the appropriate value.


 
+
 const isArrayLike = val =>
   try {return [...val], true; }
   catch (e)  { return false; }
diff --git a/snippets/isArrayLike.md b/snippets/isArrayLike.md
index eff62aae0..f55ad8328 100644
--- a/snippets/isArrayLike.md
+++ b/snippets/isArrayLike.md
@@ -7,6 +7,7 @@ Use the spread operator (`...`) to check if the provided argument is iterable in
 ```js
 
 
+
 const isArrayLike = val =>
   try {return [...val], true; }
   catch (e)  { return false; }
diff --git a/snippets/sumPower.md b/snippets/sumPower.md
index 763917f0a..f5a9087f4 100644
--- a/snippets/sumPower.md
+++ b/snippets/sumPower.md
@@ -7,12 +7,15 @@ Omit the second argument, `power`, to use a default power of `2`.
 Omit the third argument, `start`, to use a default starting value of `1`.
 
 ```js
-const sumPower = (end,power = 2,start = 1) =>
-  Array(end + 1 - start).fill(0).map((x,i) => (i+start) ** power).reduce((a,b) => a+b,0)
+const sumPower = (end, power = 2, start = 1) =>
+  Array(end + 1 - start)
+    .fill(0)
+    .map((x, i) => (i + start) ** power)
+    .reduce((a, b) => a + b, 0);
 ```
 
 ```js
 sumPower(10); // 385
-sumPower(10,3); //3025
-sumPower(10,3,5); //2925
+sumPower(10, 3); //3025
+sumPower(10, 3, 5); //2925
 ```