|
1 | 1 | // Implement a function repeatStr |
2 | 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 | | - |
7 | | -// Case: handle multiple repetitions: |
8 | | -// Given a target string `str` and a positive integer `count` greater than 1, |
9 | | -// When the repeatStr function is called with these inputs, |
10 | | -// Then it should return a string that contains the original `str` repeated `count` times. |
11 | | - |
12 | 3 | test("should repeat the string count times", () => { |
13 | 4 | const str = "hello"; |
14 | 5 | const count = 3; |
15 | | - const repeatedStr = repeatStr(str, count); |
| 6 | + const repeatedStr = repeatStr(count, str); |
16 | 7 | expect(repeatedStr).toEqual("hellohellohello"); |
17 | 8 | }); |
| 9 | +// test("should return the original string when count is 1", () => { |
| 10 | +// const str = "hello"; |
| 11 | +// const count = 1; |
| 12 | +// const repeatedStr = repeatStr(str, count); |
| 13 | +// expect(repeatedStr).toEqual("hello"); |
| 14 | +// }); |
| 15 | +// test("should return an empty string when count is 0", () => { |
| 16 | +// const str = "hello"; |
| 17 | +// const count = 0; |
| 18 | +// const repeatedStr = repeatStr(str, count); |
| 19 | +// expect(repeatedStr).toEqual(""); |
| 20 | +// }); |
| 21 | +// // Given a target string `str` and a positive integer `count`, |
| 22 | +// // When the repeatStr function is called with these inputs, |
| 23 | +// // Then it should: |
| 24 | +// // - Validate that `count` is a non-negative integer. |
| 25 | +// // - If `count` is 0, return an empty string. |
| 26 | +// // - If `count` is 1, return the original `str`. |
| 27 | +// // - If `count` is greater than 1, concatenate `str` to itself `count` times and return the resulting string. |
18 | 28 |
|
19 | | -// Case: handle count of 1: |
20 | | -// Given a target string `str` and a `count` equal to 1, |
21 | | -// When the repeatStr function is called with these inputs, |
22 | | -// Then it should return the original `str` without repetition. |
23 | | - |
24 | | -// Case: Handle count of 0: |
25 | | -// Given a target string `str` and a `count` equal to 0, |
26 | | -// When the repeatStr function is called with these inputs, |
27 | | -// Then it should return an empty string. |
| 29 | +// // Case: handle multiple repetitions: |
| 30 | +// // Given a target string `str` and a positive integer `count` greater than 1, |
| 31 | +// // When the repeatStr function is called with these inputs, |
| 32 | +// // Then it should return a string that contains the original `str` repeated `count` times. |
| 33 | +// test("should repeat the string count times", () => { |
| 34 | +// const str = "hello"; |
| 35 | +// const count = 3; |
| 36 | +// const repeatedStr = repeatStr(str, count); |
| 37 | +// expect(repeatedStr).toEqual("hellohellohello"); |
| 38 | +// }); |
| 39 | +// // Case: handle count of 1: |
| 40 | +// // Given a target string `str` and a `count` equal to 1, |
| 41 | +// // When the repeatStr function is called with these inputs, |
| 42 | +// test("should repeat the string count times", () => { |
| 43 | +// const str = "hello"; |
| 44 | +// const count = 3; |
| 45 | +// const repeatedStr = repeatStr(str, count); |
| 46 | +// expect(repeatedStr).toEqual("hellohellohello"); |
| 47 | +// }); |
28 | 48 |
|
29 | | -// Case: Handle negative count: |
30 | | -// Given a target string `str` and a negative integer `count`, |
31 | | -// When the repeatStr function is called with these inputs, |
32 | | -// Then it should throw an error, as negative counts are not valid. |
| 49 | +// // Case: handle count of 1: |
| 50 | +// // Given a target string `str` and a `count` equal to 1, |
| 51 | +// // When the repeatStr function is called with these inputs, |
| 52 | +// // Then it should return the original `str` without repetition. |
| 53 | +// test("should return the original string when count is 1", () => { |
| 54 | +// const str = "hello"; |
| 55 | +// const count = 1; |
| 56 | +// const repeatedStr = repeatStr(str, count); |
| 57 | +// expect(repeatedStr).toEqual("hello"); |
| 58 | +// }); |
| 59 | +// // Case: handle count of 0: |
| 60 | +// // Given a target string `str` and a `count` equal to 0, |
| 61 | +// // When the repeatStr function is called with these inputs, |
| 62 | +// // Then it should return an empty string. |
| 63 | +// test("should return the original string when count is 1", () => { |
| 64 | +// const str = "hello"; |
| 65 | +// const count = 1; |
| 66 | +// const repeatedStr = repeatStr(str, count); |
| 67 | +// expect(repeatedStr).toEqual("hello"); |
| 68 | +// }); |
| 69 | +// // Case: Handle count of 0: |
| 70 | +// // Given a target string `str` and a `count` equal to 0, |
| 71 | +// // When the repeatStr function is called with these inputs, |
| 72 | +// // Then it should return an empty string. |
| 73 | +// test("should return an empty string when count is 0", () => { |
| 74 | +// const str = "hello"; |
| 75 | +// const count = 0; |
| 76 | +// const repeatedStr = repeatStr(str, count); |
| 77 | +// expect(repeatedStr).toEqual(""); |
| 78 | +// }); |
| 79 | +// // Case: Handle negative count: |
| 80 | +// // Given a target string `str` and a negative integer `count`, |
| 81 | +// // When the repeatStr function is called with these inputs, |
| 82 | +// // Then it should throw an error, as negative counts are not valid. |
| 83 | +// test("should throw an error when count is negative", () => { |
| 84 | +// const str = "hello"; |
| 85 | +// const count = -1; |
| 86 | +// expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); |
| 87 | +// }); |
| 88 | +// // Case: Handle non-integer count: |
| 89 | +// // Given a target string `str` and a non-integer value for `count`, |
| 90 | +// // When the repeatStr function is called with these inputs, |
| 91 | +// // Then it should throw an error, as non-integer counts are not valid. |
| 92 | +// test("should throw an error when count is not an integer", () => { |
| 93 | +// const str = "hello"; |
| 94 | +// const count = 2.5; |
| 95 | +// expect(() => repeatStr(str, count)).toThrow("Count must be a non-negative integer"); |
| 96 | +// }); |
0 commit comments