make min max average and sum snippets
This commit is contained in:
@ -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
|
||||
```
|
||||
@ -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
|
||||
```
|
||||
@ -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
13
snippets/average.md
Normal 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
13
snippets/max.md
Normal 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
|
||||
```
|
||||
@ -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
13
snippets/sum.md
Normal 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
|
||||
```
|
||||
Reference in New Issue
Block a user