File tree Expand file tree Collapse file tree 2 files changed +23
-2
lines changed
Expand file tree Collapse file tree 2 files changed +23
-2
lines changed Original file line number Diff line number Diff line change 1- function repeat ( ) {
2- return "hellohellohello" ;
1+ function repeat ( str , count ) {
2+ if ( count < 0 ) {
3+ return "Count must be a non-negative integer"
4+ }
5+ return str . repeat ( count ) ;
36}
47
58module . exports = repeat ;
Original file line number Diff line number Diff line change @@ -21,12 +21,30 @@ test("should repeat the string count times", () => {
2121// When the repeat function is called with these inputs,
2222// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
2323
24+ test ( "should return original string if count is 1" , ( ) => {
25+ const str = "hello" ;
26+ const count = 1 ;
27+ expect ( repeat ( str , count ) ) . toEqual ( "hello" ) ;
28+ } ) ;
29+
2430// case: Handle Count of 0:
2531// Given a target string str and a count equal to 0,
2632// When the repeat function is called with these inputs,
2733// Then it should return an empty string, ensuring that a count of 0 results in an empty output.
2834
35+ test ( "should return empty string if count is 0" , ( ) => {
36+ const str = "hello" ;
37+ const count = 0 ;
38+ expect ( repeat ( str , count ) ) . toEqual ( "" ) ;
39+ } ) ;
40+
2941// case: Negative Count:
3042// Given a target string str and a negative integer count,
3143// When the repeat function is called with these inputs,
3244// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45+
46+ test ( "should return error message for negative count" , ( ) => {
47+ const str = "hello" ;
48+ const count = - 2 ;
49+ expect ( repeat ( str , count ) ) . toEqual ( "Count must be a non-negative integer" ) ;
50+ } ) ;
You can’t perform that action at this time.
0 commit comments