|
1 | | -// Implement a function repeatStr |
2 | | -const repeatStr = require("./repeat-str"); |
3 | | -// Given a target string str and a positive integer count, |
4 | | -// When the repeatStr function is called with these inputs, |
5 | | -// Then it should: |
6 | | -function repeat(str, count) { |
7 | | - if (count < 0) { |
8 | | - throw new Error("Count must be a non-negative integer"); |
9 | | - } |
10 | | - return str.repeat(count); |
11 | | -} |
12 | | - |
13 | | -module.exports = repeat; |
14 | | - |
15 | | -// case: repeat String: |
16 | | -// Given a target string str and a positive integer count, |
17 | | -// When the repeatStr function is called with these inputs, |
18 | | -// Then it should repeat the str count times and return a new string containing the repeated str values. |
19 | 1 |
|
20 | | -test("should repeat the string count times", () => { |
21 | | - const str = "hello"; |
22 | | - const count = 3; |
23 | | - const repeatedStr = repeatStr(str, count); |
24 | | - expect(repeatedStr).toEqual("hellohellohello"); |
25 | | -}); |
26 | 2 |
|
27 | | -// case: handle Count of 1: |
28 | | -// Given a target string str and a count equal to 1, |
29 | | -// When the repeatStr function is called with these inputs, |
30 | | -// Then it should return the original str without repetition, ensuring that a count of 1 results in no repetition. |
31 | | - |
32 | | -// case: Handle Count of 0: |
33 | | -// Given a target string str and a count equal to 0, |
34 | | -// When the repeatStr 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. |
36 | | - |
37 | | -// case: Negative Count: |
38 | | -// Given a target string str and a negative integer count, |
39 | | -// When the repeatStr function is called with these inputs, |
40 | | -// Then it should throw an error or return an appropriate error message, as negative counts are not valid. |
41 | | -const repeat = require("./repeat"); |
| 3 | +const repeatStr = require("./repeat-str"); |
42 | 4 |
|
43 | | -// Case 1: Repeat string multiple times |
44 | 5 | test("should repeat the string count times", () => { |
45 | | - const str = "hello"; |
46 | | - const count = 3; |
47 | | - const repeatedStr = repeat(str, count); |
48 | | - expect(repeatedStr).toEqual("hellohellohello"); |
| 6 | + expect(repeatStr("hello", 3)).toBe("hellohellohello"); |
49 | 7 | }); |
50 | 8 |
|
51 | | -// Case 2: Handle count of 1 |
52 | 9 | test("should return the original string when count is 1", () => { |
53 | | - const str = "world"; |
54 | | - const count = 1; |
55 | | - const repeatedStr = repeat(str, count); |
56 | | - expect(repeatedStr).toEqual("world"); |
| 10 | + expect(repeatStr("world", 1)).toBe("world"); |
57 | 11 | }); |
58 | 12 |
|
59 | | -// Case 3: Handle count of 0 |
60 | 13 | test("should return an empty string when count is 0", () => { |
61 | | - const str = "test"; |
62 | | - const count = 0; |
63 | | - const repeatedStr = repeat(str, count); |
64 | | - expect(repeatedStr).toEqual(""); |
| 14 | + expect(repeatStr("test", 0)).toBe(""); |
65 | 15 | }); |
66 | 16 |
|
67 | | -// Case 4: Negative count |
68 | 17 | test("should throw an error when count is negative", () => { |
69 | | - const str = "oops"; |
70 | | - const count = -2; |
71 | | - expect(() => repeat(str, count)).toThrow("Count must be a non-negative integer"); |
| 18 | + expect(() => repeatStr("oops", -2)).toThrow("Count must be a non-negative integer"); |
72 | 19 | }); |
| 20 | + |
0 commit comments