Merge branch 'master' into remove/mathMinMax
This commit is contained in:
@ -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`.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user