Merge branch 'master' into master

This commit is contained in:
Angelos Chalaris
2017-12-12 11:07:46 +02:00
committed by GitHub
4 changed files with 29 additions and 22 deletions

View File

@ -20,6 +20,7 @@
* [Even or odd number](#even-or-odd-number)
* [Factorial](#factorial)
* [Fibonacci array generator](#fibonacci-array-generator)
* [Filter out non uniqe values in an array](#filter-out-non-uniqe-values-in-an-array)
* [Flatten array](#flatten-array)
* [Greatest common divisor (GCD)](#greatest-common-divisor-gcd)
* [Head of list](#head-of-list)
@ -118,11 +119,10 @@ var difference = (arr, values) =>
### Distance between two points
Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points.
Use `Math.hypot()` to calculate the Euclidean distance between two points.
```js
var distance = x0, y0, x1, y1 =>
Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2))
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
```
### Escape regular expression
@ -167,6 +167,15 @@ var fibonacci = n =>
},[]);
```
### Filter out non-unique values in an array
Use `Array.filter()` for an array containing only the unique values.
```js
const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
// unique([1,2,2,3,4,4,5]) -> [1,3,5]
```
### Flatten array
Use recursion.
@ -358,16 +367,12 @@ var tail = arr => arr.slice(1);
### Unique values of array
Use `reduce()` to accumulate all unique values in an array.
Check if each value has already been added, using `includes()` on the accumulator array.
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
```js
var uniqueValues = arr =>
arr.reduce( (acc, val) => {
if(!acc.includes(val))
acc.push(val);
return acc;
}, []);
const unique = arr => [...new Set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
```
### URL parameters

View File

@ -1,8 +1,7 @@
### Distance between two points
Use `Math.pow()` and `Math.sqrt()` to calculate the Euclidean distance between two points.
Use `Math.hypot()` to calculate the Euclidean distance between two points.
```js
var distance = x0, y0, x1, y1 =>
Math.sqrt(Math.pow(x1-x0, 2) + Math.pow(y1 - y0, 2))
const distance = (x0, y0, x1, y1) => Math.hypot(x1 - x0, y1 - y0);
```

View File

@ -0,0 +1,8 @@
### Filter out non-unique values in an array
Use `Array.filter()` for an array containing only the unique values.
```js
const unique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexOf(i));
// unique([1,2,2,3,4,4,5]) -> [1,3,5]
```

View File

@ -1,13 +1,8 @@
### Unique values of array
Use `reduce()` to accumulate all unique values in an array.
Check if each value has already been added, using `indexOf()` on the accumulator array.
Use ES6 `Set` and the `...rest` operator to discard all duplicated values.
```js
var uniqueValues = arr =>
arr.reduce( (acc, val) => {
if(acc.indexOf(val) === -1)
acc.push(val);
return acc;
}, []);
const unique = arr => [...new Set(arr)];
// unique([1,2,2,3,4,4,5]) -> [1,2,3,4,5]
```