forked from Chalarangelo/30-seconds-of-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Chalarangelo#468 from kriadmin/patch-2
Create geometricProgression.md
- Loading branch information
Showing
1 changed file
with
20 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
### geometricProgression | ||
|
||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive and the ratio between two terms is `step`. | ||
Returns an error if `step` equals `1`. | ||
|
||
Use `Array.from()`, `Math.log()` and `Math.floor()` to create an array of the desired length, `Array.map()` to fill with the desired values in a range. | ||
Omit the second argument, `start`, to use a default value of `1`. | ||
Omit the third argument, `step`, to use a default value of `2`. | ||
|
||
``` js | ||
const geometricProgression = (end, start = 1,step = 2) => | ||
Array.from({ length:Math.floor(Math.log(end/start)/Math.log(step))+1}).map((v, i) => start * (step ** (i)) ) | ||
``` | ||
|
||
```js | ||
geometricProgression(256); // [1, 2, 4, 8, 16, 32, 64, 128, 256] | ||
geometricProgression(256,3); //[3, 6, 12, 24, 48, 96, 192] | ||
geometricProgression(256,1,4); //[1, 4, 16, 64, 256] | ||
geometricProgression(256,2,1); //Gives error | ||
``` |