Skip to content

Commit 801006e

Browse files
committed
added the function repeatString
1 parent 05b00c9 commit 801006e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Implement a function repeat
2+
3+
// Given a target string str and a positive integer count,
4+
// When the repeat function is called with these inputs,
5+
// Then it should:
6+
7+
// case: repeat String:
8+
// Given a target string str and a positive integer count,
9+
// When the repeat function is called with these inputs,
10+
// Then it should repeat the str count times and return a new string containing the repeated str values.
11+
12+
// case: handle Count of 1:
13+
// Given a target string str and a count equal to 1,
14+
// When the repeat function is called with these inputs,
15+
// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
16+
17+
// case: Handle Count of 0:
18+
// Given a target string str and a count equal to 0,
19+
// When the repeat function is called with these inputs,
20+
// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
21+
22+
// case: Negative Count:
23+
// Given a target string str and a negative integer count,
24+
// When the repeat function is called with these inputs,
25+
// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
26+
27+
function repeatString(str, count){
28+
let newWords = "";
29+
//if count is greater than 1 if so repeat string count times
30+
//else if count is 1 return the string
31+
//else if count is 0 return empty string
32+
//else if count is negative return error
33+
if(count > 1){
34+
for(let i = 0; i < count; i++){
35+
newWords = newWords + str + " "
36+
37+
}
38+
return newWords.trimEnd();
39+
}
40+
else if(count === 0){
41+
return "";
42+
}
43+
else if(count < 0 ){
44+
console.error("error your number is negative");
45+
46+
}
47+
48+
}
49+
50+
console.log(repeatString("karla", -1))

0 commit comments

Comments
 (0)