11// Implement a function repeat
2- const repeat = require ( "./repeat" ) ;
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:
62
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.
3+ const repeat = require ( "./repeat" ) ;
114
125test ( "should repeat the string count times" , ( ) => {
13- const str = "hello" ;
14- const count = 3 ;
15- const repeatedStr = repeat ( str , count ) ;
16- expect ( repeatedStr ) . toEqual ( "hellohellohello" ) ;
6+ expect ( repeat ( "hello" , 3 ) ) . toEqual ( "hellohellohello" ) ;
177} ) ;
188
19- // case: handle Count of 1:
20- // Given a target string str and a count equal to 1,
21- // When the repeat function is called with these inputs,
22- // Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition.
23- test ( "should return original string when count is 1" , ( ) => {
24- const str = "hello" ;
25- const count = 1 ;
26- const repeatedStr = repeat ( str , count ) ;
27- expect ( repeatedStr ) . toEqual ( "hello" ) ;
9+ test ( "should return the original string when count is 1" , ( ) => {
10+ expect ( repeat ( "hello" , 1 ) ) . toEqual ( "hello" ) ;
2811} ) ;
2912
30-
31-
32- // case: Handle Count of 0:
33- // Given a target string str and a count equal to 0,
34- // When the repeat function is called with these inputs,
35- // Then it should return an empty string, ensuring that a count of 0 results in an empty output.
3613test ( "should return an empty string when count is 0" , ( ) => {
37- const str = "world" ;
38- const count = 0 ;
39- const repeatedStr = repeat ( str , count ) ;
40- expect ( repeatedStr ) . toEqual ( "" ) ;
14+ expect ( repeat ( "hello" , 0 ) ) . toEqual ( "" ) ;
4115} ) ;
4216
43-
44-
45- // case: Negative Count:
46- // Given a target string str and a negative integer count,
47- // When the repeat function is called with these inputs,
48- // Then it should throw an error or return an appropriate error message, as negative counts are not valid.
4917test ( "should throw an error when count is negative" , ( ) => {
50- const str = "test" ;
51- const count = - 3 ;
52- expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Count must be non-negative" ) ;
53- } ) ;
18+ expect ( ( ) => repeat ( "hello" , - 2 ) ) . toThrow ( "Count must be non-negative" ) ;
19+ } ) ;
20+
0 commit comments