Skip to content

Commit 9f51c39

Browse files
committed
Add an intermediate (repeate str n times) problem
1 parent a44773b commit 9f51c39

File tree

6 files changed

+1475
-0
lines changed

6 files changed

+1475
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 0006 Repate a String Num times
2+
3+
repeat a given string `str` for `num` of times, and return empty in case the num is zero or negative number.
4+
5+
#### Note: you are not allowd to use `.repeat()` method.
6+
7+
## Test case:
8+
9+
- Input: 'abc', 3 => Output: 'abcabcabc'
10+
- Input: 'abc', 0 => Output: ''
11+
- Input: 'abc', -2 => Output: ''
12+
13+
## Solution:
14+
15+
>- JavaScript.
16+
17+
```
18+
const repeatStringNumTimes = (str, num) => {
19+
if (num < 1) {
20+
return "";
21+
}
22+
// here I used recursion technique to solve the problem
23+
return str + repeatStringNumTimes(str, num - 1);
24+
};
25+
```
26+
27+
>- [click](https://www.khanacademy.org/computing/computer-science/algorithms/recursive-algorithms/a/recursion) To lern more about recursion method
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const repeatStringNumTimes = (str, num) => {
2+
if (num < 1) {
3+
return "";
4+
}
5+
// here I used recursion technique to solve the problem
6+
return str + repeatStringNumTimes(str, num - 1);
7+
};
8+
9+
// console.log(repeatStringNumTimes("AaA ", 3));
10+
11+
module.exports = repeatStringNumTimes;

0 commit comments

Comments
 (0)