Skip to content

Commit 6a021e3

Browse files
implemented repeat function with error handling and edge cases with comments detailing.
1 parent 234e8b6 commit 6a021e3

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(str, count) {
2+
// check if "count" is a negative number if so will throw new error
3+
if (count < 0) {
4+
throw new error("Count must be a positive number");
5+
6+
//check if the is equal to zero
7+
if (count === 0){
8+
return ""; // returns empty string if count is equal to zero
9+
}
10+
//check if the count is equals 1.
11+
if (count === 1) {
12+
//returns just the string as its not needed to be repeated
13+
return str;
14+
}
15+
return str.repeat(count); // if the count is above two it repeat "count" number of times.
16+
}
317
}
418

519
module.exports = repeatStr;

0 commit comments

Comments
 (0)