Skip to content

Commit 7661229

Browse files
committed
implement repeatStr function
1 parent 3c69efb commit 7661229

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed
Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
if (count === 0) {
3+
return "";
4+
} else if (count === 1) {
5+
return str;
6+
} else if (count > 1) {
7+
return str.repeat(count);
8+
} else {
9+
throw new Error("Count cannot be a negative number");
10+
}
311
}
12+
// I can make the 3 valid cases in 1 case with the repeat method as folows:
13+
// function repeatStr(str, count) {
14+
// if (count >= 0) {
15+
// return str.repeat(count);
16+
// } else {
17+
// throw new Error("Count cannot be a negative number");
18+
// }
19+
// }
420

521
module.exports = repeatStr;

0 commit comments

Comments
 (0)