Skip to content

Commit d4aba95

Browse files
Implement repeatStr function to handle string repetition based on input count
1 parent 1a5ddc6 commit d4aba95

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed
Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1-
function repeatStr() {
2-
return "hellohellohello";
1+
function repeatStr(stringToRepeat, numberOfRepetitions) {
2+
// Handle count of 0
3+
if (numberOfRepetitions === 0) {
4+
return "";
5+
}
6+
7+
// Handle count of 1
8+
if (numberOfRepetitions === 1) {
9+
return stringToRepeat;
10+
}
11+
12+
// Handle multiple repetitions (> than 1)
13+
if (numberOfRepetitions > 1) {
14+
let finalString = "";
15+
16+
// loop for repeating the string and concatenating it to the final string
17+
for (let repetition = 0; repetition < numberOfRepetitions; repetition++) {
18+
finalString = finalString + stringToRepeat;
19+
}
20+
21+
return finalString;
22+
}
323
}
424

525
module.exports = repeatStr;

0 commit comments

Comments
 (0)