File tree Expand file tree Collapse file tree 2 files changed +15
-7
lines changed
Expand file tree Collapse file tree 2 files changed +15
-7
lines changed Original file line number Diff line number Diff line change 11function repeat ( str , count ) {
2- if ( count < 0 ) {
3- return false ;
2+ if ( typeof count !== "number" || isNaN ( count ) || count < 0 ) {
3+ throw new Error ( "Count must be a valid number" ) ;
44 }
55 return str . repeat ( count ) ;
66}
Original file line number Diff line number Diff line change @@ -42,9 +42,17 @@ test("should repeat the string 0 times ( means the output will be an empty strin
4242// Given a target string str and a negative integer count,
4343// When the repeat function is called with these inputs,
4444// Then it should throw an error or return an appropriate error message, as negative counts are not valid.
45- test ( "should repeat the string 0 times ( means the output will be an empty string) " , ( ) => {
45+ test ( "should throw an error if count is not a valid number " , ( ) => {
4646 const str = "hello" ;
47- const count = - 3 ;
48- const repeatedStr = repeat ( str , count ) ;
49- expect ( repeatedStr ) . toEqual ( false ) ;
50- } ) ;
47+ const count = - 8 ;
48+ expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Count must be a valid number" ) ;
49+ } ) ;
50+
51+ test ( "should throw an error if count is not a valid number" , ( ) => {
52+ const str = "hello" ;
53+ const count = "abc" ;
54+ expect ( ( ) => repeat ( str , count ) ) . toThrow ( "Count must be a valid number" ) ;
55+ } ) ;
56+
57+
58+
You can’t perform that action at this time.
0 commit comments