make min max average and sum snippets

This commit is contained in:
Rohit Tanwar
2017-12-29 16:59:49 +05:30
parent d74df3dc8f
commit f77a6b1585
7 changed files with 42 additions and 42 deletions

View File

@ -1,13 +0,0 @@
### arrayAverage
Returns the average of an array of numbers.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
```js
const arrayAverage = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
```
```js
arrayAverage([1, 2, 3]); // 2
```

View File

@ -1,13 +0,0 @@
### arrayMax
Returns the maximum value in an array.
Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array.
```js
const arrayMax = arr => Math.max(...arr);
```
```js
arrayMax([10, 1, 5]); // 10
```

View File

@ -1,13 +0,0 @@
### arraySum
Returns the sum of an array of numbers.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
```js
const arraySum = arr => arr.reduce((acc, val) => acc + val, 0);
```
```js
arraySum([1, 2, 3, 4]); // 10
```

13
snippets/average.md Normal file
View File

@ -0,0 +1,13 @@
### average
Returns the average of an of two or more numbers/arrays.
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
```js
const average = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0) / arr.length;
```
```js
average([1, 2, 3]); // 2
```

13
snippets/max.md Normal file
View File

@ -0,0 +1,13 @@
### max
Returns the maximum value out of two or more numbers/arrays.
Use `Math.max()` combined with the spread operator (`...`) to get the maximum value in the array.
```js
const max = (...arr) => Math.max(...[].concat(...arr);
```
```js
max([10, 1, 5]); // 10
```

View File

@ -1,13 +1,13 @@
### arrayMin
### min
Returns the minimum value in an array.
Use `Math.min()` combined with the spread operator (`...`) to get the minimum value in the array.
```js
const arrayMin = arr => Math.min(...arr);
const min = arr => Math.min(...[].concat(...arr));
```
```js
arrayMin([10, 1, 5]); // 1
min([10, 1, 5]); // 1
```

13
snippets/sum.md Normal file
View File

@ -0,0 +1,13 @@
### 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`.
```js
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
```
```js
sum([1, 2, 3, 4]); // 10
```