Merge branch 'master' into remove/mathMinMax
This commit is contained in:
59
README.md
59
README.md
@ -1,7 +1,9 @@
|
||||

|
||||
|
||||
# 30 seconds of code
|
||||
[](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [](https://github.com/Flet/semistandard)
|
||||
|
||||
[](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [](https://github.com/Flet/semistandard) [](https://www.producthunt.com/posts/30-seconds-of-code)
|
||||
|
||||
|
||||
> Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
|
||||
|
||||
@ -283,6 +285,16 @@
|
||||
|
||||
</details>
|
||||
|
||||
### _Uncategorized_
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
|
||||
* [`maxN`](#maxn)
|
||||
* [`minN`](#minn)
|
||||
|
||||
</details>
|
||||
|
||||
---
|
||||
## 🔌 Adapter
|
||||
|
||||
@ -3253,7 +3265,7 @@ round(1.005, 2); // 1.01
|
||||
### solveRPN
|
||||
|
||||
Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation).
|
||||
Throws appropriate errors if there are unrecognized symbols or the expression is wrong.
|
||||
Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- `+`,`-`,`*`,`/`,`^`,`**` (`^`&`**` are the exponential symbols and are same). This snippet does not supports any unary operators.
|
||||
|
||||
Use a dictionary, `OPERATORS` to specify each operator's matching mathematical operation.
|
||||
Use `String.replace()` with a regular expression to replace `^` with `**`, `String.split()` to tokenize the string and `Array.filter()` to remove empty tokens.
|
||||
@ -3340,7 +3352,7 @@ standardDeviation([10, 2, 38, 23, 38, 23, 21], true); // 12.29899614287479 (popu
|
||||
|
||||
### sum
|
||||
|
||||
Returns the sum of an of two or more numbers/arrays.
|
||||
Returns the sum of two or more numbers/arrays.
|
||||
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
@ -5127,6 +5139,47 @@ yesNo('Foo', true); // true
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
---
|
||||
## _Uncategorized_
|
||||
|
||||
### maxN
|
||||
|
||||
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
|
||||
|
||||
Sort's the array's shallow copy in descending order and returns the first n elements
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
|
||||
```
|
||||
|
||||
```js
|
||||
maxN([1, 2, 3]); // [3]
|
||||
maxN([1, 2, 3], 2); // [3,2]
|
||||
maxN([1, 2, 3], 4); // [3,2,1]
|
||||
```
|
||||
|
||||
<br>[⬆ back to top](#table-of-contents)
|
||||
|
||||
|
||||
### minN
|
||||
|
||||
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
|
||||
|
||||
Sort's the array's shallow copy in ascending order and returns the first n elements
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
|
||||
```
|
||||
```js
|
||||
minN([1, 2, 3]); // [1]
|
||||
minN([1, 2, 3], 2); // [1,2]
|
||||
minN([1, 2, 3], 4); // [1,2,3]
|
||||
```
|
||||
|
||||
<br>[⬆ back to top](#table-of-contents)
|
||||
|
||||
|
||||
## Collaborators
|
||||
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -1,16 +1,18 @@
|
||||
### initializeArrayWithRange
|
||||
|
||||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive.
|
||||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`.
|
||||
|
||||
Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
||||
Use `Array(Math.ceil((end+1-start)/step)` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range.
|
||||
You can omit `start` to use a default value of `0`.
|
||||
You can omit `step` to use a default value of `1`.
|
||||
|
||||
```js
|
||||
const initializeArrayWithRange = (end, start = 0) =>
|
||||
Array.from({ length: end + 1 - start }).map((v, i) => i + start);
|
||||
const initializeArrayWithRange = (end, start = 0,step = 1) =>
|
||||
Array.from({length:Math.ceil((end + 1 - start)/step)}).map((v, i) => (i * step) + start);
|
||||
```
|
||||
|
||||
```js
|
||||
initializeArrayWithRange(5); // [0,1,2,3,4,5]
|
||||
initializeArrayWithRange(7, 3); // [3,4,5,6,7]
|
||||
initializeArrayWithRange(9,0,2); //[0,2,4,6,8]
|
||||
```
|
||||
|
||||
16
snippets/maxN.md
Normal file
16
snippets/maxN.md
Normal file
@ -0,0 +1,16 @@
|
||||
### maxN
|
||||
|
||||
Returns the `n` maximum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in descending order).
|
||||
|
||||
Sort's the array's shallow copy in descending order and returns the first n elements
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n);
|
||||
```
|
||||
|
||||
```js
|
||||
maxN([1, 2, 3]); // [3]
|
||||
maxN([1, 2, 3], 2); // [3,2]
|
||||
maxN([1, 2, 3], 4); // [3,2,1]
|
||||
```
|
||||
15
snippets/minN.md
Normal file
15
snippets/minN.md
Normal file
@ -0,0 +1,15 @@
|
||||
### minN
|
||||
|
||||
Returns the `n` minimum elements from the provided array. If `n` is greater than or equal to the provided array's length than return the original array(sorted in ascending order).
|
||||
|
||||
Sort's the array's shallow copy in ascending order and returns the first n elements
|
||||
|
||||
Skip the second argument to get a single element(in the form of a array)
|
||||
```js
|
||||
const minN = (arr, n = 1) => [...arr].sort((a, b) => a - b).slice(0, n);
|
||||
```
|
||||
```js
|
||||
minN([1, 2, 3]); // [1]
|
||||
minN([1, 2, 3], 2); // [1,2]
|
||||
minN([1, 2, 3], 4); // [1,2,3]
|
||||
```
|
||||
@ -1,7 +1,7 @@
|
||||
### solveRPN
|
||||
|
||||
Solves the given mathematical expression in [reverse polish notation](https://en.wikipedia.org/wiki/Reverse_Polish_notation).
|
||||
Throws appropriate errors if there are unrecognized symbols or the expression is wrong.
|
||||
Throws appropriate errors if there are unrecognized symbols or the expression is wrong. The valid operators are :- `+`,`-`,`*`,`/`,`^`,`**` (`^`&`**` are the exponential symbols and are same). This snippet does not supports any unary operators.
|
||||
|
||||
Use a dictionary, `OPERATORS` to specify each operator's matching mathematical operation.
|
||||
Use `String.replace()` with a regular expression to replace `^` with `**`, `String.split()` to tokenize the string and `Array.filter()` to remove empty tokens.
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### sum
|
||||
|
||||
Returns the sum of an of two or more numbers/arrays.
|
||||
Returns the sum of two or more numbers/arrays.
|
||||
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
|
||||
@ -1,7 +1,9 @@
|
||||

|
||||
|
||||
# 30 seconds of code
|
||||
[](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [](https://github.com/Flet/semistandard)
|
||||
|
||||
[](https://github.com/Chalarangelo/30-seconds-of-code/blob/master/LICENSE) [](https://gitter.im/30-seconds-of-code/Lobby) [](http://makeapullrequest.com) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code) [](https://insight.io/github.com/Chalarangelo/30-seconds-of-code/tree/master/?source=0) [](https://github.com/Flet/semistandard) [](https://www.producthunt.com/posts/30-seconds-of-code)
|
||||
|
||||
|
||||
> Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user