add minN and maxN
This commit is contained in:
19
snippets/maxN.md
Normal file
19
snippets/maxN.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
### 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) => {
|
||||||
|
let rra = [...arr].sort((a,b) => b-a)
|
||||||
|
return rra.splice(0,n)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
``` js
|
||||||
|
maxN([1,2,3]); // [3]
|
||||||
|
maxN([1,2,3],2); // [3,2]
|
||||||
|
maxN([1,2,3],4); // [3,2,1]
|
||||||
|
```
|
||||||
18
snippets/minN.md
Normal file
18
snippets/minN.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
### 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) => {
|
||||||
|
let rra = [...arr].sort((a,b) => a-b)
|
||||||
|
return rra.splice(0,n)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
``` js
|
||||||
|
maxN([1,2,3]); // [1]
|
||||||
|
maxN([1,2,3],2); // [1,2]
|
||||||
|
maxN([1,2,3],4); // [1,2,3]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user