11function repeatStr ( n , str ) {
2+ if ( ! Number . isInteger ( n ) || n < 0 ) {
3+ throw new Error ( "Count must be a non-negative integer" ) ;
4+ }
25 let result = "" ;
36 for ( let i = 0 ; i < n ; i ++ ) {
47 result += str ;
@@ -7,32 +10,3 @@ function repeatStr(n, str) {
710}
811
912module . exports = repeatStr ;
10- // const repeatStr = require("./repeat-str");
11- // Given a target string `str` and a positive integer `count`,
12- // When the repeatStr function is called with these inputs,
13- // Then it should:
14- // test("repeats a string n times", () => {
15- // expect(repeatStr(3, "hello")).toBe("hellohellohello");
16- // });
17- const repeatStr = require ( "./repeat-str" ) ;
18- test ( "repeats a string n times" , ( ) => {
19- expect ( repeatStr ( 3 , "hello" ) ) . toBe ( "hellohellohello" ) ;
20- } ) ;
21- // test("repeats a different string", () => {
22- // expect(repeatStr(2, "abc")).toBe("abcabc");
23- // });
24- test ( "repeats a different string" , ( ) => {
25- expect ( repeatStr ( 2 , "abc" ) ) . toBe ( "abcabc" ) ;
26- } ) ;
27- // test("returns empty string when n is 0", () => {
28- // expect(repeatStr(0, "hi")).toBe("");
29- // });
30- test ( "returns empty string when n is 0" , ( ) => {
31- expect ( repeatStr ( 0 , "hi" ) ) . toBe ( "" ) ;
32- } ) ;
33- // test("returns empty string when n is 0", () => {
34- // expect(repeatStr(0, "hi")).toBe("");
35- // });
36- test ( "returns empty string when n is 0" , ( ) => {
37- expect ( repeatStr ( 0 , "hi" ) ) . toBe ( "" ) ;
38- } ) ;
0 commit comments