|
1 | | -# 0006 Repate a String Num times |
| 1 | +# 0006 Repate a String Num times ( L-I ) |
| 2 | +## Problem |
2 | 3 |
|
3 | | -repeat a given string `str` for `num` of times, and return empty in case the num is zero or negative number. |
| 4 | +repeat a given string `str` for `num` of times, and return empty in case the num is zero or negative number, and without using `.repeat()` method. |
4 | 5 |
|
5 | | -#### Note: you are not allowd to use `.repeat()` method. |
| 6 | +## Test Cases |
6 | 7 |
|
7 | | -## For testing: |
| 8 | +- str = "A" and num = 5, the function should return "AAAAA". |
| 9 | +- str = "A" and num = 0, the function should return "". |
| 10 | +- str = "A" and num = 2, the function should return "AA". |
| 11 | +- str = "A" and num = -1, the function should return "". |
| 12 | +## Solution |
8 | 13 |
|
9 | | -- run |
10 | | -``` |
11 | | -npm i |
12 | | -``` |
13 | | -to install mocha library, then run |
14 | | -``` |
15 | | -npm test |
16 | | -``` |
17 | | - |
18 | | -## Solution: |
19 | | - |
20 | | ->- JavaScript. |
21 | | -
|
22 | | -``` |
| 14 | +```javascript |
23 | 15 | const repeatStringNumTimes = (str, num) => { |
24 | 16 | if (num < 1) { |
25 | 17 | return ""; |
26 | 18 | } |
27 | 19 | // here I used recursion technique to solve the problem |
28 | 20 | return str + repeatStringNumTimes(str, num - 1); |
29 | 21 | }; |
| 22 | + |
| 23 | +console.log(repeatStringNumTimes("abc", 3)); |
30 | 24 | ``` |
31 | 25 |
|
32 | | -[click](https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursion) To lern more about recursion method |
| 26 | +## How it works |
| 27 | + |
| 28 | +- The function takes two parameters. |
| 29 | +- The first is a string, and the second a integer number. |
| 30 | +- If the the second parameter is number and positive, it will return the string * number of times. |
| 31 | + |
| 32 | +## References |
| 33 | + |
| 34 | +- [Khan Academy](https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursion) |
| 35 | +- [FreeCodeCamp](https://www.freecodecamp.org/news/three-ways-to-repeat-a-string-in-javascript-2a9053b93a2d/) |
| 36 | + |
| 37 | +## Problem Added By |
| 38 | +- [GitHub](https://github.com/ibr5500) |
| 39 | +- [LinkedIn](https://www.linkedin.com/in/ibrahim-ahmat/) |
| 40 | +- [Twitter](https://twitter.com/ibr_ahmat) |
| 41 | + |
| 42 | +## Contributing |
| 43 | +Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. |
33 | 44 |
|
| 45 | +Please make sure to update tests as appropriate. |
0 commit comments